@haklex/rich-renderer-mention 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Innei
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+
24
+ Additional Terms and Conditions
25
+
26
+ ----------------
27
+
28
+ Use of this software is governed by the terms of MIT and, in addition, by the terms and conditions described in the additional file (ADDITIONAL_TERMS.md). By using this software, you agree to abide by these additional terms and conditions.
package/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # @haklex/rich-renderer-mention
2
+
3
+ 提及渲染器,支持多平台徽章。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ pnpm add @haklex/rich-renderer-mention @haklex/rich-editor
9
+ ```
10
+
11
+ ## 导出
12
+
13
+ ```ts
14
+ export { MentionRenderer } from './MentionRenderer'
15
+ export { MentionEditRenderer } from './MentionEditRenderer'
16
+ ```
17
+
18
+ ## 使用
19
+
20
+ ```tsx
21
+ import { MentionRenderer } from '@haklex/rich-renderer-mention'
22
+ import type { RendererConfig } from '@haklex/rich-editor'
23
+
24
+ const config: RendererConfig = {
25
+ Mention: MentionRenderer,
26
+ }
27
+ ```
28
+
29
+ ## License
30
+
31
+ MIT
@@ -0,0 +1,3 @@
1
+ import { MentionRendererProps } from './MentionRenderer';
2
+ export declare function MentionEditRenderer(props: MentionRendererProps): import("react/jsx-runtime").JSX.Element;
3
+ //# sourceMappingURL=MentionEditRenderer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MentionEditRenderer.d.ts","sourceRoot":"","sources":["../src/MentionEditRenderer.tsx"],"names":[],"mappings":"AAAA,OAAO,cAAc,CAAA;AASrB,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAA;AAO7D,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,oBAAoB,2CAQ9D"}
@@ -0,0 +1,15 @@
1
+ export interface MentionRendererProps {
2
+ platform: string;
3
+ handle: string;
4
+ displayName?: string;
5
+ }
6
+ interface MentionPlatformMeta {
7
+ label: string;
8
+ icon: React.ReactNode;
9
+ getUrl: (handle: string) => string;
10
+ }
11
+ export declare const platformMetaMap: Record<string, MentionPlatformMeta>;
12
+ export declare const platformKeys: string[];
13
+ export declare function MentionRenderer({ platform, handle, displayName, }: MentionRendererProps): import("react/jsx-runtime").JSX.Element;
14
+ export {};
15
+ //# sourceMappingURL=MentionRenderer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MentionRenderer.d.ts","sourceRoot":"","sources":["../src/MentionRenderer.tsx"],"names":[],"mappings":"AAAA,OAAO,cAAc,CAAA;AASrB,MAAM,WAAW,oBAAoB;IACnC,QAAQ,EAAE,MAAM,CAAA;IAChB,MAAM,EAAE,MAAM,CAAA;IACd,WAAW,CAAC,EAAE,MAAM,CAAA;CACrB;AAED,UAAU,mBAAmB;IAC3B,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,KAAK,CAAC,SAAS,CAAA;IACrB,MAAM,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,MAAM,CAAA;CACnC;AAOD,eAAO,MAAM,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE,mBAAmB,CAsB/D,CAAA;AAED,eAAO,MAAM,YAAY,UAA+B,CAAA;AAExD,wBAAgB,eAAe,CAAC,EAC9B,QAAQ,EACR,MAAM,EACN,WAAW,GACZ,EAAE,oBAAoB,2CA4BtB"}
@@ -0,0 +1,4 @@
1
+ export { MentionEditRenderer } from './MentionEditRenderer';
2
+ export { MentionRenderer } from './MentionRenderer';
3
+ export { MentionRenderer as default } from './MentionRenderer';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAA;AAC3D,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EAAE,eAAe,IAAI,OAAO,EAAE,MAAM,mBAAmB,CAAA"}
package/dist/index.mjs ADDED
@@ -0,0 +1,269 @@
1
+ import { jsxs, jsx } from "react/jsx-runtime";
2
+ import { useRendererMode } from "@haklex/rich-editor";
3
+ import { Popover, PopoverTrigger, PopoverPanel } from "@haklex/rich-editor-ui";
4
+ import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
5
+ import { $getNearestNodeFromDOMNode } from "lexical";
6
+ import { AtSign, User, ExternalLink, Trash2 } from "lucide-react";
7
+ import { useRef, useState, useEffect, useCallback } from "react";
8
+ import { SiZhihu, SiTelegram, SiX, SiGithub } from "@icons-pack/react-simple-icons";
9
+ const GitHubIcon = /* @__PURE__ */ jsx(SiGithub, { size: "1em", className: "rich-mention-icon-gh" });
10
+ const TwitterIcon = /* @__PURE__ */ jsx(SiX, { size: "1em", color: "#1DA1F2" });
11
+ const TelegramIcon = /* @__PURE__ */ jsx(SiTelegram, { size: "1em", color: "#2AABEE" });
12
+ const ZhihuIcon = /* @__PURE__ */ jsx(SiZhihu, { size: "1em", color: "#0084FF" });
13
+ const platformMetaMap = {
14
+ GH: {
15
+ label: "GitHub",
16
+ icon: GitHubIcon,
17
+ getUrl: (handle) => `https://github.com/${encodeURIComponent(handle)}`
18
+ },
19
+ TW: {
20
+ label: "Twitter",
21
+ icon: TwitterIcon,
22
+ getUrl: (handle) => `https://twitter.com/${encodeURIComponent(handle)}`
23
+ },
24
+ TG: {
25
+ label: "Telegram",
26
+ icon: TelegramIcon,
27
+ getUrl: (handle) => `https://t.me/${encodeURIComponent(handle)}`
28
+ },
29
+ ZH: {
30
+ label: "Zhihu",
31
+ icon: ZhihuIcon,
32
+ getUrl: (handle) => `https://www.zhihu.com/people/${encodeURIComponent(handle)}`
33
+ }
34
+ };
35
+ const platformKeys = Object.keys(platformMetaMap);
36
+ function MentionRenderer({
37
+ platform,
38
+ handle,
39
+ displayName
40
+ }) {
41
+ const normalizedHandle = handle.replace(/^@+/, "");
42
+ const meta = platformMetaMap[platform];
43
+ const label = displayName || normalizedHandle;
44
+ if (meta) {
45
+ return /* @__PURE__ */ jsxs("span", { className: "rich-mention", children: [
46
+ /* @__PURE__ */ jsx("span", { className: "rich-mention-icon", "aria-hidden": true, children: meta.icon }),
47
+ /* @__PURE__ */ jsx(
48
+ "a",
49
+ {
50
+ className: "rich-mention-handle",
51
+ href: meta.getUrl(normalizedHandle),
52
+ target: "_blank",
53
+ rel: "noopener noreferrer",
54
+ children: label
55
+ }
56
+ )
57
+ ] });
58
+ }
59
+ return /* @__PURE__ */ jsx("span", { className: "rich-mention rich-mention-plain", children: /* @__PURE__ */ jsxs("span", { className: "rich-mention-handle", children: [
60
+ "@",
61
+ label
62
+ ] }) });
63
+ }
64
+ function MentionEditRenderer(props) {
65
+ const mode = useRendererMode();
66
+ if (mode !== "editor") {
67
+ return /* @__PURE__ */ jsx(MentionRenderer, { ...props });
68
+ }
69
+ return /* @__PURE__ */ jsx(MentionEditRendererInner, { ...props });
70
+ }
71
+ function MentionEditRendererInner({
72
+ platform,
73
+ handle,
74
+ displayName
75
+ }) {
76
+ const [editor] = useLexicalComposerContext();
77
+ const editable = editor.isEditable();
78
+ const wrapperRef = useRef(null);
79
+ const [open, setOpen] = useState(false);
80
+ const [editPlatform, setEditPlatform] = useState(platform);
81
+ const [editHandle, setEditHandle] = useState(handle);
82
+ const [editDisplayName, setEditDisplayName] = useState(displayName || "");
83
+ useEffect(() => {
84
+ setEditPlatform(platform);
85
+ setEditHandle(handle);
86
+ setEditDisplayName(displayName || "");
87
+ }, [platform, handle, displayName]);
88
+ const commitChanges = useCallback(() => {
89
+ const trimmedHandle = editHandle.trim();
90
+ if (!trimmedHandle) return;
91
+ if (!wrapperRef.current) return;
92
+ editor.update(() => {
93
+ const node = $getNearestNodeFromDOMNode(wrapperRef.current);
94
+ if (!node) return;
95
+ const writable = node.getWritable();
96
+ writable.__platform = editPlatform;
97
+ writable.__handle = trimmedHandle;
98
+ writable.__displayName = editDisplayName.trim() || void 0;
99
+ });
100
+ setOpen(false);
101
+ }, [editor, editPlatform, editHandle, editDisplayName]);
102
+ const handleDelete = useCallback(() => {
103
+ if (!wrapperRef.current) return;
104
+ editor.update(() => {
105
+ const node = $getNearestNodeFromDOMNode(wrapperRef.current);
106
+ if (node) node.remove();
107
+ });
108
+ setOpen(false);
109
+ }, [editor]);
110
+ const handleOpen = useCallback(() => {
111
+ const normalizedHandle = handle.replace(/^@+/, "");
112
+ const meta = platformMetaMap[platform];
113
+ if (meta) {
114
+ window.open(
115
+ meta.getUrl(normalizedHandle),
116
+ "_blank",
117
+ "noopener,noreferrer"
118
+ );
119
+ }
120
+ }, [platform, handle]);
121
+ const handleKeyDown = useCallback(
122
+ (e) => {
123
+ if (e.key === "Enter") {
124
+ e.preventDefault();
125
+ commitChanges();
126
+ } else if (e.key === "Escape") {
127
+ e.preventDefault();
128
+ setEditPlatform(platform);
129
+ setEditHandle(handle);
130
+ setEditDisplayName(displayName || "");
131
+ setOpen(false);
132
+ }
133
+ },
134
+ [commitChanges, platform, handle, displayName]
135
+ );
136
+ if (!editable) {
137
+ return /* @__PURE__ */ jsx(
138
+ MentionRenderer,
139
+ {
140
+ platform,
141
+ handle,
142
+ displayName
143
+ }
144
+ );
145
+ }
146
+ return /* @__PURE__ */ jsxs(
147
+ Popover,
148
+ {
149
+ open,
150
+ onOpenChange: (nextOpen) => {
151
+ setOpen(nextOpen);
152
+ if (!nextOpen) {
153
+ setEditPlatform(platform);
154
+ setEditHandle(handle);
155
+ setEditDisplayName(displayName || "");
156
+ }
157
+ },
158
+ children: [
159
+ /* @__PURE__ */ jsx(
160
+ PopoverTrigger,
161
+ {
162
+ delay: 200,
163
+ closeDelay: 300,
164
+ openOnHover: true,
165
+ render: /* @__PURE__ */ jsx("span", { ref: wrapperRef, className: "rich-mention-edit-trigger" }),
166
+ children: /* @__PURE__ */ jsx(
167
+ MentionRenderer,
168
+ {
169
+ platform,
170
+ handle,
171
+ displayName
172
+ }
173
+ )
174
+ }
175
+ ),
176
+ /* @__PURE__ */ jsxs(
177
+ PopoverPanel,
178
+ {
179
+ side: "bottom",
180
+ sideOffset: 8,
181
+ className: "rich-mention-edit-panel",
182
+ children: [
183
+ /* @__PURE__ */ jsxs("div", { className: "rich-mention-edit-field", children: [
184
+ /* @__PURE__ */ jsx(
185
+ "span",
186
+ {
187
+ className: "rich-mention-edit-field-icon",
188
+ style: { fontSize: 14 },
189
+ "aria-hidden": true,
190
+ children: platformMetaMap[editPlatform]?.icon ?? /* @__PURE__ */ jsx(AtSign, { size: 14 })
191
+ }
192
+ ),
193
+ /* @__PURE__ */ jsx(
194
+ "select",
195
+ {
196
+ className: "rich-mention-edit-select",
197
+ value: editPlatform,
198
+ onChange: (e) => setEditPlatform(e.target.value),
199
+ children: platformKeys.map((key) => /* @__PURE__ */ jsx("option", { value: key, children: platformMetaMap[key].label }, key))
200
+ }
201
+ )
202
+ ] }),
203
+ /* @__PURE__ */ jsxs("div", { className: "rich-mention-edit-field", children: [
204
+ /* @__PURE__ */ jsx(AtSign, { className: "rich-mention-edit-field-icon", size: 14 }),
205
+ /* @__PURE__ */ jsx(
206
+ "input",
207
+ {
208
+ className: "rich-mention-edit-input",
209
+ type: "text",
210
+ value: editHandle,
211
+ onChange: (e) => setEditHandle(e.target.value),
212
+ onKeyDown: handleKeyDown,
213
+ placeholder: "handle"
214
+ }
215
+ )
216
+ ] }),
217
+ /* @__PURE__ */ jsxs("div", { className: "rich-mention-edit-field", children: [
218
+ /* @__PURE__ */ jsx(User, { className: "rich-mention-edit-field-icon", size: 14 }),
219
+ /* @__PURE__ */ jsx(
220
+ "input",
221
+ {
222
+ className: "rich-mention-edit-input",
223
+ type: "text",
224
+ value: editDisplayName,
225
+ onChange: (e) => setEditDisplayName(e.target.value),
226
+ onBlur: commitChanges,
227
+ onKeyDown: handleKeyDown,
228
+ placeholder: "Display name (optional)"
229
+ }
230
+ )
231
+ ] }),
232
+ /* @__PURE__ */ jsxs("div", { className: "rich-mention-edit-actions", children: [
233
+ /* @__PURE__ */ jsxs(
234
+ "button",
235
+ {
236
+ className: "rich-mention-edit-action-btn",
237
+ type: "button",
238
+ onClick: handleOpen,
239
+ children: [
240
+ /* @__PURE__ */ jsx(ExternalLink, { size: 14 }),
241
+ "Open"
242
+ ]
243
+ }
244
+ ),
245
+ /* @__PURE__ */ jsxs(
246
+ "button",
247
+ {
248
+ className: "rich-mention-edit-action-btn rich-mention-edit-action-btn--end",
249
+ type: "button",
250
+ onClick: handleDelete,
251
+ children: [
252
+ /* @__PURE__ */ jsx(Trash2, { size: 14 }),
253
+ "Remove"
254
+ ]
255
+ }
256
+ )
257
+ ] })
258
+ ]
259
+ }
260
+ )
261
+ ]
262
+ }
263
+ );
264
+ }
265
+ export {
266
+ MentionEditRenderer,
267
+ MentionRenderer,
268
+ MentionRenderer as default
269
+ };
@@ -0,0 +1,112 @@
1
+ .rich-mention {
2
+ display: inline;
3
+ margin: 0 0.25em;
4
+ vertical-align: text-bottom;
5
+ white-space: nowrap;
6
+ }
7
+ .rich-mention-icon {
8
+ display: inline-flex;
9
+ align-items: center;
10
+ margin-right: 0.25em;
11
+ vertical-align: middle;
12
+ }
13
+ .rich-mention-icon svg {
14
+ display: inline;
15
+ height: 0.8em;
16
+ width: 0.8em;
17
+ }
18
+ .rich-mention-icon-gh {
19
+ fill: currentColor;
20
+ }
21
+ .rich-mention-handle {
22
+ text-decoration-line: underline;
23
+ text-underline-offset: 2px;
24
+ color: inherit;
25
+ }
26
+ .rich-mention-handle:hover {
27
+ color: var(--rc-accent);
28
+ }
29
+ .rich-mention-plain {
30
+ color: var(--rc-text-secondary);
31
+ }
32
+ .rich-mention-edit-panel {
33
+ display: flex;
34
+ flex-direction: column;
35
+ gap: 8px;
36
+ width: 300px;
37
+ padding: 12px;
38
+ font-size: 13px;
39
+ font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
40
+ }
41
+ .rich-mention-edit-field {
42
+ display: flex;
43
+ align-items: center;
44
+ gap: 8px;
45
+ padding: 6px 10px;
46
+ background-color: var(--rc-bg-secondary);
47
+ border-radius: 6px;
48
+ min-width: 0;
49
+ }
50
+ .rich-mention-edit-trigger {
51
+ display: inline-flex;
52
+ align-items: center;
53
+ justify-content: center;
54
+ }
55
+ .rich-mention-edit-field-icon {
56
+ display: inline-flex;
57
+ align-items: center;
58
+ justify-content: center;
59
+ }
60
+ .rich-mention-edit-input {
61
+ flex: 1;
62
+ appearance: none;
63
+ border: none;
64
+ background-color: transparent;
65
+ color: inherit;
66
+ font-size: 13px;
67
+ padding: 0;
68
+ outline: none;
69
+ min-width: 0;
70
+ }
71
+ .rich-mention-edit-input::placeholder {
72
+ color: var(--rc-text-secondary);
73
+ }
74
+ .rich-mention-edit-select {
75
+ flex: 1;
76
+ appearance: none;
77
+ border: none;
78
+ background-color: transparent;
79
+ color: inherit;
80
+ font-size: 13px;
81
+ padding: 0;
82
+ outline: none;
83
+ cursor: pointer;
84
+ min-width: 0;
85
+ }
86
+ .rich-mention-edit-actions {
87
+ display: flex;
88
+ align-items: center;
89
+ gap: 4px;
90
+ }
91
+ .rich-mention-edit-action-btn {
92
+ display: inline-flex;
93
+ align-items: center;
94
+ gap: 6px;
95
+ appearance: none;
96
+ border: none;
97
+ background: none;
98
+ color: inherit;
99
+ font-size: 13px;
100
+ font-weight: 500;
101
+ cursor: pointer;
102
+ padding: 4px 8px;
103
+ border-radius: 4px;
104
+ transition: color 0.15s ease, background-color 0.15s ease;
105
+ white-space: nowrap;
106
+ }
107
+ .rich-mention-edit-action-btn:hover {
108
+ background-color: var(--rc-bg-secondary);
109
+ }
110
+ .rich-mention-edit-action-btn--end {
111
+ margin-left: auto;
112
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=styles.css.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"styles.css.d.ts","sourceRoot":"","sources":["../src/styles.css.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@haklex/rich-renderer-mention",
3
+ "version": "0.0.1",
4
+ "description": "Social mention renderer with platform badges",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.mjs",
10
+ "types": "./dist/index.d.ts"
11
+ },
12
+ "./style.css": "./dist/rich-renderer-mention.css"
13
+ },
14
+ "main": "./dist/index.mjs",
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "dependencies": {
19
+ "@icons-pack/react-simple-icons": "^13.11.2",
20
+ "lucide-react": "^0.574.0"
21
+ },
22
+ "devDependencies": {
23
+ "@lexical/react": "^0.39.0",
24
+ "@types/react": "^19.0.0",
25
+ "@types/react-dom": "^19.0.0",
26
+ "@vanilla-extract/css": "^1.17.1",
27
+ "@vanilla-extract/vite-plugin": "^4.0.20",
28
+ "lexical": "^0.39.0",
29
+ "react": "19.2.4",
30
+ "react-dom": "19.2.4",
31
+ "typescript": "^5.9.0",
32
+ "vite": "^7.3.1",
33
+ "vite-plugin-dts": "^4.5.0",
34
+ "@haklex/rich-editor": "0.0.1",
35
+ "@haklex/rich-editor-ui": "0.0.1",
36
+ "@haklex/rich-style-token": "0.0.1"
37
+ },
38
+ "peerDependencies": {
39
+ "@lexical/react": "^0.39.0",
40
+ "lexical": "^0.39.0",
41
+ "react": ">=19",
42
+ "react-dom": ">=19",
43
+ "@haklex/rich-editor": "0.0.1",
44
+ "@haklex/rich-editor-ui": "0.0.1",
45
+ "@haklex/rich-style-token": "0.0.1"
46
+ },
47
+ "publishConfig": {
48
+ "access": "public"
49
+ },
50
+ "scripts": {
51
+ "build": "vite build",
52
+ "dev:build": "vite build --watch"
53
+ },
54
+ "types": "./dist/index.d.ts"
55
+ }