Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/editors/slate/elements/mention/index.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 React from "react";
14
import { SlateElement, register, RenderElementProps } from "../register";
15
16
export interface Mention extends SlateElement {
17
type: "mention";
18
account_id: string;
19
name: string;
20
isInline: true;
21
isVoid: true;
22
}
23
24
const STYLE = {
25
color: "#0958d9",
26
background: "white",
27
borderRadius: "3px",
28
padding: "3px",
29
margin: "-3px 0",
30
} as React.CSSProperties;
31
32
const StaticElement: React.FC<RenderElementProps> = ({
33
attributes,
34
element,
35
}) => {
36
if (element.type != "mention") {
37
throw Error("bug");
38
}
39
return (
40
<span {...attributes} style={STYLE}>
41
@{element.name}
42
</span>
43
);
44
};
45
46
export function createMentionStatic(account_id: string, name?: string) {
47
if (name == null) {
48
name = "User";
49
}
50
return {
51
type: "mention" as "mention",
52
isVoid: true as true,
53
isInline: true as true,
54
account_id,
55
name: name as string,
56
children: [{ text: "" }],
57
};
58
}
59
60
register({
61
slateType: "mention",
62
63
toSlate: ({ token }) => {
64
const { account_id, name } = token;
65
return createMentionStatic(account_id, name);
66
},
67
68
StaticElement,
69
});
70
71