@payloadcms/richtext-lexical 3.80.0-canary.1 → 3.80.0-internal.c396c15
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/dist/exports/client/bundled.css +1 -1
- package/dist/exports/client/index.js +22 -22
- package/dist/exports/client/index.js.map +4 -4
- package/dist/features/toolbars/fixed/client/Toolbar/index.d.ts.map +1 -1
- package/dist/features/toolbars/fixed/client/Toolbar/index.js +84 -141
- package/dist/features/toolbars/fixed/client/Toolbar/index.js.map +1 -1
- package/dist/features/toolbars/inline/client/Toolbar/index.d.ts.map +1 -1
- package/dist/features/toolbars/inline/client/Toolbar/index.js +46 -101
- package/dist/features/toolbars/inline/client/Toolbar/index.js.map +1 -1
- package/dist/features/toolbars/shared/ToolbarButton/index.d.ts +3 -1
- package/dist/features/toolbars/shared/ToolbarButton/index.d.ts.map +1 -1
- package/dist/features/toolbars/shared/ToolbarButton/index.js +38 -111
- package/dist/features/toolbars/shared/ToolbarButton/index.js.map +1 -1
- package/dist/features/toolbars/shared/ToolbarDropdown/index.d.ts +4 -10
- package/dist/features/toolbars/shared/ToolbarDropdown/index.d.ts.map +1 -1
- package/dist/features/toolbars/shared/ToolbarDropdown/index.js +10 -79
- package/dist/features/toolbars/shared/ToolbarDropdown/index.js.map +1 -1
- package/dist/features/toolbars/shared/useToolbarStates.d.ts +22 -0
- package/dist/features/toolbars/shared/useToolbarStates.d.ts.map +1 -0
- package/dist/features/toolbars/shared/useToolbarStates.js +127 -0
- package/dist/features/toolbars/shared/useToolbarStates.js.map +1 -0
- package/dist/field/bundled.css +1 -1
- package/package.json +6 -6
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { mergeRegister } from '@lexical/utils';
|
|
4
|
+
import { $getSelection } from 'lexical';
|
|
5
|
+
import { useCallback, useDeferredValue, useEffect, useRef, useState } from 'react';
|
|
6
|
+
import { useEditorConfigContext } from '../../../lexical/config/client/EditorConfigProvider.js';
|
|
7
|
+
import { useRunDeprioritized } from '../../../utilities/useRunDeprioritized.js';
|
|
8
|
+
/**
|
|
9
|
+
* Build default states where every item is enabled and nothing is active.
|
|
10
|
+
* Used as the initial value so toolbar groups render immediately on mount
|
|
11
|
+
* instead of waiting for the first updateStates call.
|
|
12
|
+
*/
|
|
13
|
+
function buildDefaultStates(groups) {
|
|
14
|
+
const itemStates = new Map();
|
|
15
|
+
const groupStates = new Map();
|
|
16
|
+
if (!groups?.length) {
|
|
17
|
+
return {
|
|
18
|
+
groupStates,
|
|
19
|
+
itemStates
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
for (const group of groups) {
|
|
23
|
+
const enabledItemKeys = [];
|
|
24
|
+
for (const item of group.items) {
|
|
25
|
+
itemStates.set(item.key, {
|
|
26
|
+
active: false,
|
|
27
|
+
enabled: true
|
|
28
|
+
});
|
|
29
|
+
enabledItemKeys.push(item.key);
|
|
30
|
+
}
|
|
31
|
+
groupStates.set(group.key, {
|
|
32
|
+
activeItemKeys: [],
|
|
33
|
+
activeItems: [],
|
|
34
|
+
enabledGroup: true,
|
|
35
|
+
enabledItemKeys
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
groupStates,
|
|
40
|
+
itemStates
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Subscribes once to `editor.registerUpdateListener` and computes
|
|
45
|
+
* isActive / isEnabled for every toolbar item in a read.
|
|
46
|
+
*/
|
|
47
|
+
export function useToolbarStates(editor, groups) {
|
|
48
|
+
const [states, setStates] = useState(() => buildDefaultStates(groups));
|
|
49
|
+
const deferredStates = useDeferredValue(states);
|
|
50
|
+
const editorConfigContext = useEditorConfigContext();
|
|
51
|
+
const editorConfigContextRef = useRef(editorConfigContext);
|
|
52
|
+
editorConfigContextRef.current = editorConfigContext;
|
|
53
|
+
const groupsRef = useRef(groups);
|
|
54
|
+
groupsRef.current = groups;
|
|
55
|
+
const runDeprioritized = useRunDeprioritized();
|
|
56
|
+
const updateStates = useCallback(() => {
|
|
57
|
+
editor.getEditorState().read(() => {
|
|
58
|
+
const selection = $getSelection();
|
|
59
|
+
if (!selection) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const currentGroups = groupsRef.current;
|
|
63
|
+
if (!currentGroups?.length) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const ctx = editorConfigContextRef.current;
|
|
67
|
+
const newItemStates = new Map();
|
|
68
|
+
const newGroupStates = new Map();
|
|
69
|
+
for (const group of currentGroups) {
|
|
70
|
+
const activeItemKeys = [];
|
|
71
|
+
const activeItems = [];
|
|
72
|
+
const enabledItemKeys = [];
|
|
73
|
+
const maxActive = group.type === 'dropdown' ? group.maxActiveItems ?? 1 : undefined;
|
|
74
|
+
for (const item of group.items) {
|
|
75
|
+
const isActive = item.isActive ? (!maxActive || activeItemKeys.length < maxActive) && item.isActive({
|
|
76
|
+
editor,
|
|
77
|
+
editorConfigContext: ctx,
|
|
78
|
+
selection
|
|
79
|
+
}) : false;
|
|
80
|
+
const isEnabled = item.isEnabled ? item.isEnabled({
|
|
81
|
+
editor,
|
|
82
|
+
editorConfigContext: ctx,
|
|
83
|
+
selection
|
|
84
|
+
}) : true;
|
|
85
|
+
if (isActive) {
|
|
86
|
+
activeItemKeys.push(item.key);
|
|
87
|
+
activeItems.push(item);
|
|
88
|
+
}
|
|
89
|
+
if (isEnabled) {
|
|
90
|
+
enabledItemKeys.push(item.key);
|
|
91
|
+
}
|
|
92
|
+
newItemStates.set(item.key, {
|
|
93
|
+
active: isActive,
|
|
94
|
+
enabled: isEnabled
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
const enabledGroup = group.type === 'dropdown' && group.isEnabled ? group.isEnabled({
|
|
98
|
+
editor,
|
|
99
|
+
editorConfigContext: ctx,
|
|
100
|
+
selection
|
|
101
|
+
}) : true;
|
|
102
|
+
newGroupStates.set(group.key, {
|
|
103
|
+
activeItemKeys,
|
|
104
|
+
activeItems,
|
|
105
|
+
enabledGroup,
|
|
106
|
+
enabledItemKeys
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
setStates({
|
|
110
|
+
groupStates: newGroupStates,
|
|
111
|
+
itemStates: newItemStates
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
}, [editor]);
|
|
115
|
+
useEffect(() => {
|
|
116
|
+
void runDeprioritized(updateStates);
|
|
117
|
+
const listener = () => runDeprioritized(updateStates);
|
|
118
|
+
const cleanup = mergeRegister(editor.registerUpdateListener(listener));
|
|
119
|
+
document.addEventListener('mouseup', listener);
|
|
120
|
+
return () => {
|
|
121
|
+
cleanup();
|
|
122
|
+
document.removeEventListener('mouseup', listener);
|
|
123
|
+
};
|
|
124
|
+
}, [editor, runDeprioritized, updateStates]);
|
|
125
|
+
return deferredStates;
|
|
126
|
+
}
|
|
127
|
+
//# sourceMappingURL=useToolbarStates.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useToolbarStates.js","names":["mergeRegister","$getSelection","useCallback","useDeferredValue","useEffect","useRef","useState","useEditorConfigContext","useRunDeprioritized","buildDefaultStates","groups","itemStates","Map","groupStates","length","group","enabledItemKeys","item","items","set","key","active","enabled","push","activeItemKeys","activeItems","enabledGroup","useToolbarStates","editor","states","setStates","deferredStates","editorConfigContext","editorConfigContextRef","current","groupsRef","runDeprioritized","updateStates","getEditorState","read","selection","currentGroups","ctx","newItemStates","newGroupStates","maxActive","type","maxActiveItems","undefined","isActive","isEnabled","listener","cleanup","registerUpdateListener","document","addEventListener","removeEventListener"],"sources":["../../../../src/features/toolbars/shared/useToolbarStates.ts"],"sourcesContent":["'use client'\nimport type { LexicalEditor } from 'lexical'\n\nimport { mergeRegister } from '@lexical/utils'\nimport { $getSelection } from 'lexical'\nimport { useCallback, useDeferredValue, useEffect, useRef, useState } from 'react'\n\nimport type { ToolbarGroup, ToolbarGroupItem } from '../types.js'\n\nimport { useEditorConfigContext } from '../../../lexical/config/client/EditorConfigProvider.js'\nimport { useRunDeprioritized } from '../../../utilities/useRunDeprioritized.js'\n\nexport interface ToolbarItemState {\n active: boolean\n enabled: boolean\n}\n\nexport interface ToolbarGroupState {\n activeItemKeys: string[]\n activeItems: ToolbarGroupItem[]\n enabledGroup: boolean\n enabledItemKeys: string[]\n}\n\nexport interface ToolbarStates {\n groupStates: Map<string, ToolbarGroupState>\n itemStates: Map<string, ToolbarItemState>\n}\n\n/**\n * Build default states where every item is enabled and nothing is active.\n * Used as the initial value so toolbar groups render immediately on mount\n * instead of waiting for the first updateStates call.\n */\nfunction buildDefaultStates(groups: ToolbarGroup[] | undefined): ToolbarStates {\n const itemStates = new Map<string, ToolbarItemState>()\n const groupStates = new Map<string, ToolbarGroupState>()\n\n if (!groups?.length) {\n return { groupStates, itemStates }\n }\n\n for (const group of groups) {\n const enabledItemKeys: string[] = []\n for (const item of group.items) {\n itemStates.set(item.key, { active: false, enabled: true })\n enabledItemKeys.push(item.key)\n }\n groupStates.set(group.key, {\n activeItemKeys: [],\n activeItems: [],\n enabledGroup: true,\n enabledItemKeys,\n })\n }\n\n return { groupStates, itemStates }\n}\n\n/**\n * Subscribes once to `editor.registerUpdateListener` and computes\n * isActive / isEnabled for every toolbar item in a read.\n */\nexport function useToolbarStates(\n editor: LexicalEditor,\n groups: ToolbarGroup[] | undefined,\n): ToolbarStates {\n const [states, setStates] = useState<ToolbarStates>(() => buildDefaultStates(groups))\n const deferredStates = useDeferredValue(states)\n\n const editorConfigContext = useEditorConfigContext()\n const editorConfigContextRef = useRef(editorConfigContext)\n editorConfigContextRef.current = editorConfigContext\n\n const groupsRef = useRef(groups)\n groupsRef.current = groups\n\n const runDeprioritized = useRunDeprioritized()\n\n const updateStates = useCallback(() => {\n editor.getEditorState().read(() => {\n const selection = $getSelection()\n if (!selection) {\n return\n }\n\n const currentGroups = groupsRef.current\n if (!currentGroups?.length) {\n return\n }\n\n const ctx = editorConfigContextRef.current\n const newItemStates = new Map<string, ToolbarItemState>()\n const newGroupStates = new Map<string, ToolbarGroupState>()\n\n for (const group of currentGroups) {\n const activeItemKeys: string[] = []\n const activeItems: ToolbarGroupItem[] = []\n const enabledItemKeys: string[] = []\n\n const maxActive = group.type === 'dropdown' ? (group.maxActiveItems ?? 1) : undefined\n\n for (const item of group.items) {\n const isActive = item.isActive\n ? (!maxActive || activeItemKeys.length < maxActive) &&\n item.isActive({ editor, editorConfigContext: ctx, selection })\n : false\n\n const isEnabled = item.isEnabled\n ? item.isEnabled({ editor, editorConfigContext: ctx, selection })\n : true\n\n if (isActive) {\n activeItemKeys.push(item.key)\n activeItems.push(item)\n }\n if (isEnabled) {\n enabledItemKeys.push(item.key)\n }\n\n newItemStates.set(item.key, { active: isActive, enabled: isEnabled })\n }\n\n const enabledGroup =\n group.type === 'dropdown' && group.isEnabled\n ? group.isEnabled({ editor, editorConfigContext: ctx, selection })\n : true\n\n newGroupStates.set(group.key, {\n activeItemKeys,\n activeItems,\n enabledGroup,\n enabledItemKeys,\n })\n }\n\n setStates({ groupStates: newGroupStates, itemStates: newItemStates })\n })\n }, [editor])\n\n useEffect(() => {\n void runDeprioritized(updateStates)\n\n const listener = () => runDeprioritized(updateStates)\n\n const cleanup = mergeRegister(editor.registerUpdateListener(listener))\n document.addEventListener('mouseup', listener)\n\n return () => {\n cleanup()\n document.removeEventListener('mouseup', listener)\n }\n }, [editor, runDeprioritized, updateStates])\n\n return deferredStates\n}\n"],"mappings":"AAAA;;AAGA,SAASA,aAAa,QAAQ;AAC9B,SAASC,aAAa,QAAQ;AAC9B,SAASC,WAAW,EAAEC,gBAAgB,EAAEC,SAAS,EAAEC,MAAM,EAAEC,QAAQ,QAAQ;AAI3E,SAASC,sBAAsB,QAAQ;AACvC,SAASC,mBAAmB,QAAQ;AAmBpC;;;;;AAKA,SAASC,mBAAmBC,MAAkC;EAC5D,MAAMC,UAAA,GAAa,IAAIC,GAAA;EACvB,MAAMC,WAAA,GAAc,IA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