@haklex/rich-plugin-slash-menu 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 +28 -0
- package/README.md +44 -0
- package/dist/SlashMenuItem.d.ts +19 -0
- package/dist/SlashMenuItem.d.ts.map +1 -0
- package/dist/SlashMenuList.d.ts +10 -0
- package/dist/SlashMenuList.d.ts.map +1 -0
- package/dist/SlashMenuPlugin.d.ts +10 -0
- package/dist/SlashMenuPlugin.d.ts.map +1 -0
- package/dist/builtinItems.d.ts +3 -0
- package/dist/builtinItems.d.ts.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +317 -0
- package/dist/rich-plugin-slash-menu.css +86 -0
- package/dist/styles.css.d.ts +9 -0
- package/dist/styles.css.d.ts.map +1 -0
- package/package.json +57 -0
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,44 @@
|
|
|
1
|
+
# @haklex/rich-plugin-slash-menu
|
|
2
|
+
|
|
3
|
+
斜杠命令菜单插件。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @haklex/rich-plugin-slash-menu @haklex/rich-editor
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 导出
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
export { SlashMenuPlugin } from './SlashMenuPlugin'
|
|
15
|
+
export type { SlashMenuPluginProps } from './SlashMenuPlugin'
|
|
16
|
+
export { SlashMenuItem } from './SlashMenuItem'
|
|
17
|
+
export { SlashMenuList } from './SlashMenuList'
|
|
18
|
+
export { getBuiltinItems } from './builtinItems'
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## 使用
|
|
22
|
+
|
|
23
|
+
```tsx
|
|
24
|
+
import { SlashMenuPlugin } from '@haklex/rich-plugin-slash-menu'
|
|
25
|
+
import { RichEditor } from '@haklex/rich-editor'
|
|
26
|
+
|
|
27
|
+
<RichEditor>
|
|
28
|
+
<SlashMenuPlugin />
|
|
29
|
+
</RichEditor>
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Props
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
interface SlashMenuPluginProps {
|
|
36
|
+
items?: SlashMenuItem[] // 自定义项目
|
|
37
|
+
extraItems?: SlashMenuItem[] // 额外项目
|
|
38
|
+
triggerChar?: string // 触发字符,默认 '/'
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## License
|
|
43
|
+
|
|
44
|
+
MIT
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { MenuOption } from '@lexical/react/LexicalTypeaheadMenuPlugin';
|
|
2
|
+
import { LexicalEditor } from 'lexical';
|
|
3
|
+
import { ReactNode } from 'react';
|
|
4
|
+
export declare class SlashMenuItem extends MenuOption {
|
|
5
|
+
title: string;
|
|
6
|
+
icon: ReactNode;
|
|
7
|
+
description: string;
|
|
8
|
+
keywords: string[];
|
|
9
|
+
section: string;
|
|
10
|
+
onSelect: (editor: LexicalEditor, queryString: string) => void;
|
|
11
|
+
constructor(title: string, options: {
|
|
12
|
+
icon?: ReactNode;
|
|
13
|
+
description?: string;
|
|
14
|
+
keywords?: string[];
|
|
15
|
+
section?: string;
|
|
16
|
+
onSelect: (editor: LexicalEditor, queryString: string) => void;
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=SlashMenuItem.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SlashMenuItem.d.ts","sourceRoot":"","sources":["../src/SlashMenuItem.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,2CAA2C,CAAA;AACtE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAC5C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAEtC,qBAAa,aAAc,SAAQ,UAAU;IAC3C,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,SAAS,CAAA;IACf,WAAW,EAAE,MAAM,CAAA;IACnB,QAAQ,EAAE,MAAM,EAAE,CAAA;IAClB,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,CAAC,MAAM,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,KAAK,IAAI,CAAA;gBAG5D,KAAK,EAAE,MAAM,EACb,OAAO,EAAE;QACP,IAAI,CAAC,EAAE,SAAS,CAAA;QAChB,WAAW,CAAC,EAAE,MAAM,CAAA;QACpB,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAA;QACnB,OAAO,CAAC,EAAE,MAAM,CAAA;QAChB,QAAQ,EAAE,CAAC,MAAM,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,KAAK,IAAI,CAAA;KAC/D;CAUJ"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { SlashMenuItem } from './SlashMenuItem';
|
|
2
|
+
interface SlashMenuListProps {
|
|
3
|
+
options: SlashMenuItem[];
|
|
4
|
+
selectedIndex: number | null;
|
|
5
|
+
selectOptionAndCleanUp: (option: SlashMenuItem) => void;
|
|
6
|
+
setHighlightedIndex: (index: number) => void;
|
|
7
|
+
}
|
|
8
|
+
export declare function SlashMenuList({ options, selectedIndex, selectOptionAndCleanUp, setHighlightedIndex, }: SlashMenuListProps): import("react/jsx-runtime").JSX.Element;
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=SlashMenuList.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SlashMenuList.d.ts","sourceRoot":"","sources":["../src/SlashMenuList.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAGpD,UAAU,kBAAkB;IAC1B,OAAO,EAAE,aAAa,EAAE,CAAA;IACxB,aAAa,EAAE,MAAM,GAAG,IAAI,CAAA;IAC5B,sBAAsB,EAAE,CAAC,MAAM,EAAE,aAAa,KAAK,IAAI,CAAA;IACvD,mBAAmB,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAA;CAC7C;AAyBD,wBAAgB,aAAa,CAAC,EAC5B,OAAO,EACP,aAAa,EACb,sBAAsB,EACtB,mBAAmB,GACpB,EAAE,kBAAkB,2CA4CpB"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { LexicalEditor } from 'lexical';
|
|
2
|
+
import { SlashMenuItem } from './SlashMenuItem';
|
|
3
|
+
export interface SlashMenuPluginProps {
|
|
4
|
+
items?: SlashMenuItem[];
|
|
5
|
+
extraItems?: SlashMenuItem[];
|
|
6
|
+
triggerChar?: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function collectNodeSlashItems(editor: LexicalEditor): SlashMenuItem[];
|
|
9
|
+
export declare function SlashMenuPlugin({ items, extraItems, triggerChar, }: SlashMenuPluginProps): import("react/jsx-runtime").JSX.Element;
|
|
10
|
+
//# sourceMappingURL=SlashMenuPlugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SlashMenuPlugin.d.ts","sourceRoot":"","sources":["../src/SlashMenuPlugin.tsx"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,aAAa,EAAY,MAAM,SAAS,CAAA;AAOtD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAIpD,MAAM,WAAW,oBAAoB;IACnC,KAAK,CAAC,EAAE,aAAa,EAAE,CAAA;IACvB,UAAU,CAAC,EAAE,aAAa,EAAE,CAAA;IAC5B,WAAW,CAAC,EAAE,MAAM,CAAA;CACrB;AAWD,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,aAAa,GAAG,aAAa,EAAE,CAY5E;AAWD,wBAAgB,eAAe,CAAC,EAC9B,KAAK,EACL,UAAU,EACV,WAAiB,GAClB,EAAE,oBAAoB,2CAqEtB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builtinItems.d.ts","sourceRoot":"","sources":["../src/builtinItems.ts"],"names":[],"mappings":"AAyBA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAM/C,wBAAgB,eAAe,IAAI,aAAa,EAAE,CA2HjD"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { getBuiltinItems } from './builtinItems';
|
|
2
|
+
export { SlashMenuItem } from './SlashMenuItem';
|
|
3
|
+
export { SlashMenuList } from './SlashMenuList';
|
|
4
|
+
export type { SlashMenuPluginProps } from './SlashMenuPlugin';
|
|
5
|
+
export { collectNodeSlashItems, SlashMenuPlugin } from './SlashMenuPlugin';
|
|
6
|
+
//# 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,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAChD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,YAAY,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAA;AAC7D,OAAO,EAAE,qBAAqB,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
+
import { INSERT_UNORDERED_LIST_COMMAND, INSERT_ORDERED_LIST_COMMAND, INSERT_CHECK_LIST_COMMAND } from "@lexical/list";
|
|
5
|
+
import { INSERT_HORIZONTAL_RULE_COMMAND } from "@lexical/react/LexicalHorizontalRuleNode";
|
|
6
|
+
import { $createHeadingNode, $createQuoteNode } from "@lexical/rich-text";
|
|
7
|
+
import { $setBlocksType } from "@lexical/selection";
|
|
8
|
+
import { INSERT_TABLE_COMMAND } from "@lexical/table";
|
|
9
|
+
import { $getSelection, $isRangeSelection, $createParagraphNode } from "lexical";
|
|
10
|
+
import { Type, Heading1, Heading2, Heading3, TextQuote, Minus, Table, List, ListOrdered, ListChecks } from "lucide-react";
|
|
11
|
+
import { createElement, useMemo, createContext, use, useState, useCallback } from "react";
|
|
12
|
+
import { MenuOption, useBasicTypeaheadTriggerMatch, LexicalTypeaheadMenuPlugin } from "@lexical/react/LexicalTypeaheadMenuPlugin";
|
|
13
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
14
|
+
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
|
|
15
|
+
import { createPortal } from "react-dom";
|
|
16
|
+
class SlashMenuItem extends MenuOption {
|
|
17
|
+
constructor(title, options) {
|
|
18
|
+
super(title);
|
|
19
|
+
__publicField(this, "title");
|
|
20
|
+
__publicField(this, "icon");
|
|
21
|
+
__publicField(this, "description");
|
|
22
|
+
__publicField(this, "keywords");
|
|
23
|
+
__publicField(this, "section");
|
|
24
|
+
__publicField(this, "onSelect");
|
|
25
|
+
this.title = title;
|
|
26
|
+
this.icon = options.icon ?? "";
|
|
27
|
+
this.description = options.description ?? "";
|
|
28
|
+
this.keywords = options.keywords ?? [];
|
|
29
|
+
this.section = options.section ?? "BASIC BLOCKS";
|
|
30
|
+
this.onSelect = options.onSelect;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const SECTION_BASIC = "BASIC BLOCKS";
|
|
34
|
+
const SECTION_LIST = "LISTS";
|
|
35
|
+
const ICON_SIZE = 20;
|
|
36
|
+
function getBuiltinItems() {
|
|
37
|
+
return [
|
|
38
|
+
new SlashMenuItem("Text", {
|
|
39
|
+
icon: createElement(Type, { size: ICON_SIZE }),
|
|
40
|
+
description: "Plain text block",
|
|
41
|
+
keywords: ["paragraph", "text", "plain"],
|
|
42
|
+
section: SECTION_BASIC,
|
|
43
|
+
onSelect: (editor) => {
|
|
44
|
+
editor.update(() => {
|
|
45
|
+
const selection = $getSelection();
|
|
46
|
+
if ($isRangeSelection(selection)) {
|
|
47
|
+
$setBlocksType(selection, () => $createParagraphNode());
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}),
|
|
52
|
+
new SlashMenuItem("Heading 1", {
|
|
53
|
+
icon: createElement(Heading1, { size: ICON_SIZE }),
|
|
54
|
+
description: "Large section heading",
|
|
55
|
+
keywords: ["heading", "h1", "title"],
|
|
56
|
+
section: SECTION_BASIC,
|
|
57
|
+
onSelect: (editor) => {
|
|
58
|
+
editor.update(() => {
|
|
59
|
+
const selection = $getSelection();
|
|
60
|
+
if ($isRangeSelection(selection)) {
|
|
61
|
+
$setBlocksType(selection, () => $createHeadingNode("h1"));
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}),
|
|
66
|
+
new SlashMenuItem("Heading 2", {
|
|
67
|
+
icon: createElement(Heading2, { size: ICON_SIZE }),
|
|
68
|
+
description: "Medium section heading",
|
|
69
|
+
keywords: ["heading", "h2", "subtitle"],
|
|
70
|
+
section: SECTION_BASIC,
|
|
71
|
+
onSelect: (editor) => {
|
|
72
|
+
editor.update(() => {
|
|
73
|
+
const selection = $getSelection();
|
|
74
|
+
if ($isRangeSelection(selection)) {
|
|
75
|
+
$setBlocksType(selection, () => $createHeadingNode("h2"));
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}),
|
|
80
|
+
new SlashMenuItem("Heading 3", {
|
|
81
|
+
icon: createElement(Heading3, { size: ICON_SIZE }),
|
|
82
|
+
description: "Small section heading",
|
|
83
|
+
keywords: ["heading", "h3"],
|
|
84
|
+
section: SECTION_BASIC,
|
|
85
|
+
onSelect: (editor) => {
|
|
86
|
+
editor.update(() => {
|
|
87
|
+
const selection = $getSelection();
|
|
88
|
+
if ($isRangeSelection(selection)) {
|
|
89
|
+
$setBlocksType(selection, () => $createHeadingNode("h3"));
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}),
|
|
94
|
+
new SlashMenuItem("Quote", {
|
|
95
|
+
icon: createElement(TextQuote, { size: ICON_SIZE }),
|
|
96
|
+
description: "Capture a quote",
|
|
97
|
+
keywords: ["quote", "blockquote"],
|
|
98
|
+
section: SECTION_BASIC,
|
|
99
|
+
onSelect: (editor) => {
|
|
100
|
+
editor.update(() => {
|
|
101
|
+
const selection = $getSelection();
|
|
102
|
+
if ($isRangeSelection(selection)) {
|
|
103
|
+
$setBlocksType(selection, () => $createQuoteNode());
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}),
|
|
108
|
+
new SlashMenuItem("Divider", {
|
|
109
|
+
icon: createElement(Minus, { size: ICON_SIZE }),
|
|
110
|
+
description: "Visual separator",
|
|
111
|
+
keywords: ["divider", "hr", "rule", "separator"],
|
|
112
|
+
section: SECTION_BASIC,
|
|
113
|
+
onSelect: (editor) => {
|
|
114
|
+
editor.dispatchCommand(INSERT_HORIZONTAL_RULE_COMMAND, void 0);
|
|
115
|
+
}
|
|
116
|
+
}),
|
|
117
|
+
new SlashMenuItem("Table", {
|
|
118
|
+
icon: createElement(Table, { size: ICON_SIZE }),
|
|
119
|
+
description: "Add a table",
|
|
120
|
+
keywords: ["table", "grid"],
|
|
121
|
+
section: SECTION_BASIC,
|
|
122
|
+
onSelect: (editor) => {
|
|
123
|
+
editor.dispatchCommand(INSERT_TABLE_COMMAND, {
|
|
124
|
+
columns: "3",
|
|
125
|
+
rows: "3",
|
|
126
|
+
includeHeaders: true
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}),
|
|
130
|
+
new SlashMenuItem("Bulleted List", {
|
|
131
|
+
icon: createElement(List, { size: ICON_SIZE }),
|
|
132
|
+
description: "Unordered list",
|
|
133
|
+
keywords: ["list", "bullet", "unordered"],
|
|
134
|
+
section: SECTION_LIST,
|
|
135
|
+
onSelect: (editor) => {
|
|
136
|
+
editor.dispatchCommand(INSERT_UNORDERED_LIST_COMMAND, void 0);
|
|
137
|
+
}
|
|
138
|
+
}),
|
|
139
|
+
new SlashMenuItem("Numbered List", {
|
|
140
|
+
icon: createElement(ListOrdered, { size: ICON_SIZE }),
|
|
141
|
+
description: "Ordered list with numbers",
|
|
142
|
+
keywords: ["list", "ordered", "number"],
|
|
143
|
+
section: SECTION_LIST,
|
|
144
|
+
onSelect: (editor) => {
|
|
145
|
+
editor.dispatchCommand(INSERT_ORDERED_LIST_COMMAND, void 0);
|
|
146
|
+
}
|
|
147
|
+
}),
|
|
148
|
+
new SlashMenuItem("To-do List", {
|
|
149
|
+
icon: createElement(ListChecks, { size: ICON_SIZE }),
|
|
150
|
+
description: "Track tasks with checkboxes",
|
|
151
|
+
keywords: ["task", "todo", "checkbox", "checklist"],
|
|
152
|
+
section: SECTION_LIST,
|
|
153
|
+
onSelect: (editor) => {
|
|
154
|
+
editor.dispatchCommand(INSERT_CHECK_LIST_COMMAND, void 0);
|
|
155
|
+
}
|
|
156
|
+
})
|
|
157
|
+
];
|
|
158
|
+
}
|
|
159
|
+
var slashMenu = "qolrkf0";
|
|
160
|
+
var slashMenuSection = "qolrkf1";
|
|
161
|
+
var slashMenuItem = "qolrkf2";
|
|
162
|
+
var slashMenuItemIcon = "qolrkf3";
|
|
163
|
+
var slashMenuItemText = "qolrkf4";
|
|
164
|
+
var slashMenuItemTitle = "qolrkf5";
|
|
165
|
+
var slashMenuItemDescription = "qolrkf6";
|
|
166
|
+
var slashMenuEmpty = "qolrkf7";
|
|
167
|
+
function groupBySection(options) {
|
|
168
|
+
const groups = [];
|
|
169
|
+
const seen = /* @__PURE__ */ new Map();
|
|
170
|
+
options.forEach((item, index) => {
|
|
171
|
+
const label = item.section;
|
|
172
|
+
let group = seen.get(label);
|
|
173
|
+
if (!group) {
|
|
174
|
+
group = { label, items: [] };
|
|
175
|
+
seen.set(label, group);
|
|
176
|
+
groups.push(group);
|
|
177
|
+
}
|
|
178
|
+
group.items.push({ item, globalIndex: index });
|
|
179
|
+
});
|
|
180
|
+
return groups;
|
|
181
|
+
}
|
|
182
|
+
function SlashMenuList({
|
|
183
|
+
options,
|
|
184
|
+
selectedIndex,
|
|
185
|
+
selectOptionAndCleanUp,
|
|
186
|
+
setHighlightedIndex
|
|
187
|
+
}) {
|
|
188
|
+
const sections = useMemo(() => groupBySection(options), [options]);
|
|
189
|
+
if (options.length === 0) {
|
|
190
|
+
return /* @__PURE__ */ jsx("div", { className: slashMenu, children: /* @__PURE__ */ jsx("div", { className: slashMenuEmpty, children: "No matching commands" }) });
|
|
191
|
+
}
|
|
192
|
+
return /* @__PURE__ */ jsx("ul", { className: slashMenu, role: "listbox", children: sections.map((section) => /* @__PURE__ */ jsxs("li", { role: "presentation", children: [
|
|
193
|
+
/* @__PURE__ */ jsx("div", { className: slashMenuSection, children: section.label }),
|
|
194
|
+
/* @__PURE__ */ jsx("ul", { style: { listStyle: "none", margin: 0, padding: 0 }, role: "group", children: section.items.map(({ item, globalIndex }) => /* @__PURE__ */ jsxs(
|
|
195
|
+
"li",
|
|
196
|
+
{
|
|
197
|
+
className: slashMenuItem,
|
|
198
|
+
role: "option",
|
|
199
|
+
"aria-selected": globalIndex === selectedIndex,
|
|
200
|
+
onClick: () => selectOptionAndCleanUp(item),
|
|
201
|
+
onMouseEnter: () => setHighlightedIndex(globalIndex),
|
|
202
|
+
ref: item.setRefElement,
|
|
203
|
+
tabIndex: -1,
|
|
204
|
+
children: [
|
|
205
|
+
/* @__PURE__ */ jsx("span", { className: slashMenuItemIcon, children: item.icon }),
|
|
206
|
+
/* @__PURE__ */ jsxs("span", { className: slashMenuItemText, children: [
|
|
207
|
+
/* @__PURE__ */ jsx("span", { className: slashMenuItemTitle, children: item.title }),
|
|
208
|
+
item.description && /* @__PURE__ */ jsx("span", { className: slashMenuItemDescription, children: item.description })
|
|
209
|
+
] })
|
|
210
|
+
]
|
|
211
|
+
},
|
|
212
|
+
item.key
|
|
213
|
+
)) })
|
|
214
|
+
] }, section.label)) });
|
|
215
|
+
}
|
|
216
|
+
const PortalThemeContext = createContext({
|
|
217
|
+
className: ""
|
|
218
|
+
});
|
|
219
|
+
function usePortalTheme() {
|
|
220
|
+
return use(PortalThemeContext);
|
|
221
|
+
}
|
|
222
|
+
function PortalThemeWrapper({ children }) {
|
|
223
|
+
const { className } = usePortalTheme();
|
|
224
|
+
if (!className) return children;
|
|
225
|
+
return /* @__PURE__ */ jsx("div", { style: { display: "contents" }, className, children });
|
|
226
|
+
}
|
|
227
|
+
function collectNodeSlashItems(editor) {
|
|
228
|
+
const items = [];
|
|
229
|
+
const nodes = editor._nodes;
|
|
230
|
+
for (const { klass } of nodes.values()) {
|
|
231
|
+
const configs = klass.slashMenuItems;
|
|
232
|
+
if (configs) {
|
|
233
|
+
for (const config of configs) {
|
|
234
|
+
items.push(new SlashMenuItem(config.title, config));
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return items;
|
|
239
|
+
}
|
|
240
|
+
function filterItems(query, items) {
|
|
241
|
+
if (!query) return items;
|
|
242
|
+
const lower = query.toLowerCase();
|
|
243
|
+
return items.filter((item) => {
|
|
244
|
+
if (item.title.toLowerCase().includes(lower)) return true;
|
|
245
|
+
return item.keywords.some((kw) => kw.toLowerCase().includes(lower));
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
function SlashMenuPlugin({
|
|
249
|
+
items,
|
|
250
|
+
extraItems,
|
|
251
|
+
triggerChar = "/"
|
|
252
|
+
}) {
|
|
253
|
+
const [editor] = useLexicalComposerContext();
|
|
254
|
+
const [queryString, setQueryString] = useState(null);
|
|
255
|
+
const allItems = useMemo(() => {
|
|
256
|
+
if (items) return items;
|
|
257
|
+
const builtins = getBuiltinItems();
|
|
258
|
+
const nodeItems = collectNodeSlashItems(editor);
|
|
259
|
+
const combined = [...builtins, ...nodeItems];
|
|
260
|
+
return extraItems ? [...combined, ...extraItems] : combined;
|
|
261
|
+
}, [items, extraItems, editor]);
|
|
262
|
+
const filteredItems = useMemo(
|
|
263
|
+
() => filterItems(queryString ?? "", allItems),
|
|
264
|
+
[queryString, allItems]
|
|
265
|
+
);
|
|
266
|
+
const checkForTriggerMatch = useBasicTypeaheadTriggerMatch(triggerChar, {
|
|
267
|
+
minLength: 0
|
|
268
|
+
});
|
|
269
|
+
const onSelectOption = useCallback(
|
|
270
|
+
(option, textNodeContainingQuery, closeMenu, matchingString) => {
|
|
271
|
+
editor.update(() => {
|
|
272
|
+
if (textNodeContainingQuery) {
|
|
273
|
+
const selection = $getSelection();
|
|
274
|
+
if ($isRangeSelection(selection)) {
|
|
275
|
+
textNodeContainingQuery.remove();
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
closeMenu();
|
|
280
|
+
option.onSelect(editor, matchingString);
|
|
281
|
+
},
|
|
282
|
+
[editor]
|
|
283
|
+
);
|
|
284
|
+
return /* @__PURE__ */ jsx(
|
|
285
|
+
LexicalTypeaheadMenuPlugin,
|
|
286
|
+
{
|
|
287
|
+
onQueryChange: setQueryString,
|
|
288
|
+
onSelectOption,
|
|
289
|
+
triggerFn: checkForTriggerMatch,
|
|
290
|
+
options: filteredItems,
|
|
291
|
+
menuRenderFn: (anchorElementRef, { selectedIndex, selectOptionAndCleanUp, setHighlightedIndex }) => {
|
|
292
|
+
if (!anchorElementRef.current || filteredItems.length === 0) {
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
return createPortal(
|
|
296
|
+
/* @__PURE__ */ jsx(PortalThemeWrapper, { children: /* @__PURE__ */ jsx(
|
|
297
|
+
SlashMenuList,
|
|
298
|
+
{
|
|
299
|
+
options: filteredItems,
|
|
300
|
+
selectedIndex,
|
|
301
|
+
selectOptionAndCleanUp,
|
|
302
|
+
setHighlightedIndex
|
|
303
|
+
}
|
|
304
|
+
) }),
|
|
305
|
+
anchorElementRef.current
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
export {
|
|
312
|
+
SlashMenuItem,
|
|
313
|
+
SlashMenuList,
|
|
314
|
+
SlashMenuPlugin,
|
|
315
|
+
collectNodeSlashItems,
|
|
316
|
+
getBuiltinItems
|
|
317
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
.qolrkf0 {
|
|
2
|
+
position: absolute;
|
|
3
|
+
top: 0;
|
|
4
|
+
left: 0;
|
|
5
|
+
width: 320px;
|
|
6
|
+
max-height: 340px;
|
|
7
|
+
overflow-y: auto;
|
|
8
|
+
overflow-x: hidden;
|
|
9
|
+
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
|
10
|
+
background: var(--rc-bg);
|
|
11
|
+
border: 1px solid var(--rc-border);
|
|
12
|
+
border-radius: 10px;
|
|
13
|
+
box-shadow: 0 1px 4px rgba(0,0,0,0.04), 0 4px 16px rgba(0,0,0,0.08);
|
|
14
|
+
padding: 6px 0;
|
|
15
|
+
list-style: none;
|
|
16
|
+
margin: 0;
|
|
17
|
+
}
|
|
18
|
+
.qolrkf0::-webkit-scrollbar {
|
|
19
|
+
width: 6px;
|
|
20
|
+
}
|
|
21
|
+
.qolrkf0::-webkit-scrollbar-thumb {
|
|
22
|
+
background: rgba(0,0,0,0.1);
|
|
23
|
+
border-radius: 3px;
|
|
24
|
+
}
|
|
25
|
+
.qolrkf1 {
|
|
26
|
+
padding: 10px 14px 4px;
|
|
27
|
+
font-size: 11px;
|
|
28
|
+
font-weight: 500;
|
|
29
|
+
letter-spacing: 0.04em;
|
|
30
|
+
text-transform: uppercase;
|
|
31
|
+
color: var(--rc-text-secondary);
|
|
32
|
+
user-select: none;
|
|
33
|
+
}
|
|
34
|
+
.qolrkf2 {
|
|
35
|
+
display: flex;
|
|
36
|
+
align-items: center;
|
|
37
|
+
gap: 12px;
|
|
38
|
+
padding: 6px 10px;
|
|
39
|
+
margin: 0 6px;
|
|
40
|
+
border-radius: 8px;
|
|
41
|
+
cursor: pointer;
|
|
42
|
+
transition: background 0.1s ease;
|
|
43
|
+
}
|
|
44
|
+
.qolrkf2:hover, .qolrkf2[aria-selected="true"] {
|
|
45
|
+
background: var(--rc-bg-secondary);
|
|
46
|
+
}
|
|
47
|
+
.qolrkf3 {
|
|
48
|
+
width: 40px;
|
|
49
|
+
height: 40px;
|
|
50
|
+
display: flex;
|
|
51
|
+
align-items: center;
|
|
52
|
+
justify-content: center;
|
|
53
|
+
font-size: 18px;
|
|
54
|
+
flex-shrink: 0;
|
|
55
|
+
border-radius: 6px;
|
|
56
|
+
background: var(--rc-bg-secondary);
|
|
57
|
+
border: 1px solid var(--rc-border);
|
|
58
|
+
color: var(--rc-text-secondary);
|
|
59
|
+
line-height: 1;
|
|
60
|
+
}
|
|
61
|
+
.qolrkf4 {
|
|
62
|
+
display: flex;
|
|
63
|
+
flex-direction: column;
|
|
64
|
+
min-width: 0;
|
|
65
|
+
gap: 1px;
|
|
66
|
+
}
|
|
67
|
+
.qolrkf5 {
|
|
68
|
+
font-size: 14px;
|
|
69
|
+
font-weight: 500;
|
|
70
|
+
line-height: 1.3;
|
|
71
|
+
color: var(--rc-text);
|
|
72
|
+
}
|
|
73
|
+
.qolrkf6 {
|
|
74
|
+
font-size: 12px;
|
|
75
|
+
line-height: 1.3;
|
|
76
|
+
color: var(--rc-text-secondary);
|
|
77
|
+
white-space: nowrap;
|
|
78
|
+
overflow: hidden;
|
|
79
|
+
text-overflow: ellipsis;
|
|
80
|
+
}
|
|
81
|
+
.qolrkf7 {
|
|
82
|
+
padding: 20px 16px;
|
|
83
|
+
color: color-mix(in srgb, var(--rc-text-secondary) 70%, transparent);
|
|
84
|
+
font-size: 13px;
|
|
85
|
+
text-align: center;
|
|
86
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare const slashMenu: string;
|
|
2
|
+
export declare const slashMenuSection: string;
|
|
3
|
+
export declare const slashMenuItem: string;
|
|
4
|
+
export declare const slashMenuItemIcon: string;
|
|
5
|
+
export declare const slashMenuItemText: string;
|
|
6
|
+
export declare const slashMenuItemTitle: string;
|
|
7
|
+
export declare const slashMenuItemDescription: string;
|
|
8
|
+
export declare const slashMenuEmpty: string;
|
|
9
|
+
//# 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":"AAIA,eAAO,MAAM,SAAS,QAiBpB,CAAA;AAYF,eAAO,MAAM,gBAAgB,QAQ3B,CAAA;AAGF,eAAO,MAAM,aAAa,QASxB,CAAA;AAOF,eAAO,MAAM,iBAAiB,QAa5B,CAAA;AAGF,eAAO,MAAM,iBAAiB,QAK5B,CAAA;AAEF,eAAO,MAAM,kBAAkB,QAK7B,CAAA;AAEF,eAAO,MAAM,wBAAwB,QAOnC,CAAA;AAGF,eAAO,MAAM,cAAc,QAKzB,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@haklex/rich-plugin-slash-menu",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Slash command menu plugin",
|
|
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-plugin-slash-menu.css"
|
|
13
|
+
},
|
|
14
|
+
"main": "./dist/index.mjs",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@lexical/list": "^0.39.0",
|
|
20
|
+
"@lexical/react": "^0.39.0",
|
|
21
|
+
"@lexical/rich-text": "^0.39.0",
|
|
22
|
+
"@lexical/selection": "^0.39.0",
|
|
23
|
+
"@lexical/table": "^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
|
+
"lucide-react": "^0.574.0",
|
|
30
|
+
"react": "19.2.4",
|
|
31
|
+
"react-dom": "19.2.4",
|
|
32
|
+
"typescript": "^5.9.0",
|
|
33
|
+
"vite": "^7.3.1",
|
|
34
|
+
"vite-plugin-dts": "^4.5.0",
|
|
35
|
+
"@haklex/rich-style-token": "0.0.1"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"@lexical/list": "^0.39.0",
|
|
39
|
+
"@lexical/react": "^0.39.0",
|
|
40
|
+
"@lexical/rich-text": "^0.39.0",
|
|
41
|
+
"@lexical/selection": "^0.39.0",
|
|
42
|
+
"@lexical/table": "^0.39.0",
|
|
43
|
+
"lexical": "^0.39.0",
|
|
44
|
+
"lucide-react": "^0.574.0",
|
|
45
|
+
"react": ">=19",
|
|
46
|
+
"react-dom": ">=19",
|
|
47
|
+
"@haklex/rich-style-token": "0.0.1"
|
|
48
|
+
},
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public"
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "vite build",
|
|
54
|
+
"dev:build": "vite build --watch"
|
|
55
|
+
},
|
|
56
|
+
"types": "./dist/index.d.ts"
|
|
57
|
+
}
|