Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/slate/elements/mention/editable.tsx
1702 views
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
/* We want mentions to be represented in the markdown like this:
7
8
<span class="user-mention" account-id=47d0393e-4814-4452-bb6c-35bac4cbd314 >@Bella Welski</span>
9
10
because then they will be compatible with all mentions already used with chat and tasks.
11
*/
12
13
import { trunc_middle as truncMiddle } from "@cocalc/util/misc";
14
import { redux } from "@cocalc/frontend/app-framework";
15
import { FOCUSED_COLOR } from "../../util";
16
import { register, RenderElementProps } from "../register";
17
import { useFocused, useSelected } from "../hooks";
18
import { createMentionStatic } from "./index";
19
20
const Element: React.FC<RenderElementProps> = ({
21
attributes,
22
children,
23
element,
24
}) => {
25
if (element.type != "mention") {
26
throw Error("bug");
27
}
28
const focused = useFocused();
29
const selected = useSelected();
30
31
const border =
32
focused && selected ? `1px solid ${FOCUSED_COLOR}` : `1px solid transparent`;
33
34
return (
35
<span {...attributes}>
36
<span contentEditable={false} className="user-mention" style={{ border }}>
37
@{element.name}
38
</span>
39
{children}
40
</span>
41
);
42
};
43
44
export function createMention(account_id: string, name?: string) {
45
if (name == null) {
46
name = truncMiddle(redux.getStore("users").get_name(account_id), 64);
47
}
48
return createMentionStatic(account_id, name);
49
}
50
51
register({
52
slateType: "mention",
53
54
toSlate: ({ token }) => {
55
const { account_id, name } = token;
56
return createMention(account_id, name);
57
},
58
59
Element,
60
61
fromSlate: ({ node }) =>
62
`<span class="user-mention" account-id=${node.account_id}>@${node.name}</span>`,
63
});
64
65