Path: blob/master/src/packages/frontend/editors/slate/elements/mention/editable.tsx
1702 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/* We want mentions to be represented in the markdown like this:67<span class="user-mention" account-id=47d0393e-4814-4452-bb6c-35bac4cbd314 >@Bella Welski</span>89because then they will be compatible with all mentions already used with chat and tasks.10*/1112import { trunc_middle as truncMiddle } from "@cocalc/util/misc";13import { redux } from "@cocalc/frontend/app-framework";14import { FOCUSED_COLOR } from "../../util";15import { register, RenderElementProps } from "../register";16import { useFocused, useSelected } from "../hooks";17import { createMentionStatic } from "./index";1819const Element: React.FC<RenderElementProps> = ({20attributes,21children,22element,23}) => {24if (element.type != "mention") {25throw Error("bug");26}27const focused = useFocused();28const selected = useSelected();2930const border =31focused && selected ? `1px solid ${FOCUSED_COLOR}` : `1px solid transparent`;3233return (34<span {...attributes}>35<span contentEditable={false} className="user-mention" style={{ border }}>36@{element.name}37</span>38{children}39</span>40);41};4243export function createMention(account_id: string, name?: string) {44if (name == null) {45name = truncMiddle(redux.getStore("users").get_name(account_id), 64);46}47return createMentionStatic(account_id, name);48}4950register({51slateType: "mention",5253toSlate: ({ token }) => {54const { account_id, name } = token;55return createMention(account_id, name);56},5758Element,5960fromSlate: ({ node }) =>61`<span class="user-mention" account-id=${node.account_id}>@${node.name}</span>`,62});636465