@haklex/rich-plugin-floating-toolbar 0.0.7 → 0.0.8

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.
@@ -1 +1 @@
1
- {"version":3,"file":"FloatingToolbarPlugin.d.ts","sourceRoot":"","sources":["../src/FloatingToolbarPlugin.tsx"],"names":[],"mappings":"AAwBA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,OAAO,CAAA;AAuGzC,wBAAgB,qBAAqB,IAAI,YAAY,GAAG,IAAI,CA4L3D"}
1
+ {"version":3,"file":"FloatingToolbarPlugin.d.ts","sourceRoot":"","sources":["../src/FloatingToolbarPlugin.tsx"],"names":[],"mappings":"AAyBA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,OAAO,CAAA;AAmIzC,wBAAgB,qBAAqB,IAAI,YAAY,GAAG,IAAI,CAoN3D"}
package/dist/index.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  import { jsxs, jsx } from "react/jsx-runtime";
2
2
  import { ColorPicker } from "@haklex/rich-editor-ui";
3
+ import { usePortalTheme, vars } from "@haklex/rich-style-token";
3
4
  import { $isLinkNode, TOGGLE_LINK_COMMAND } from "@lexical/link";
4
5
  import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
5
6
  import { $patchStyleText, $getSelectionStyleValueForProperty } from "@lexical/selection";
@@ -81,8 +82,27 @@ function ToolbarButton({
81
82
  }
82
83
  const ICON_SIZE = 15;
83
84
  const ICON_STROKE = 2;
85
+ function extractCssVarName(value) {
86
+ const match = value.match(/^var\((--[^),\s]+)(?:,[^)]+)?\)$/);
87
+ return match?.[1] ?? null;
88
+ }
89
+ function collectThemeVarNames(contract, output) {
90
+ if (typeof contract === "string") {
91
+ const cssVarName = extractCssVarName(contract);
92
+ if (cssVarName) output.add(cssVarName);
93
+ return output;
94
+ }
95
+ if (contract && typeof contract === "object") {
96
+ for (const value of Object.values(contract)) {
97
+ collectThemeVarNames(value, output);
98
+ }
99
+ }
100
+ return output;
101
+ }
102
+ const THEME_VAR_NAMES = Array.from(collectThemeVarNames(vars, /* @__PURE__ */ new Set()));
84
103
  function FloatingToolbarPlugin() {
85
104
  const [editor] = useLexicalComposerContext();
105
+ const { className: portalClassName } = usePortalTheme();
86
106
  const toolbarRef = useRef(null);
87
107
  const [visible, setVisible] = useState(false);
88
108
  const [state, setState] = useState(INITIAL_STATE);
@@ -95,6 +115,20 @@ function FloatingToolbarPlugin() {
95
115
  setState(getSelectionState(selection));
96
116
  setVisible(true);
97
117
  }, []);
118
+ const applyThemeVars = useCallback(
119
+ (toolbar2) => {
120
+ const rootElement = editor.getRootElement();
121
+ if (!rootElement) return;
122
+ const computed = window.getComputedStyle(rootElement);
123
+ for (const name of THEME_VAR_NAMES) {
124
+ const value = computed.getPropertyValue(name).trim();
125
+ if (value) {
126
+ toolbar2.style.setProperty(name, value);
127
+ }
128
+ }
129
+ },
130
+ [editor]
131
+ );
98
132
  useEffect(() => {
99
133
  const unregisterCommand = editor.registerCommand(
100
134
  SELECTION_CHANGE_COMMAND,
@@ -121,6 +155,7 @@ function FloatingToolbarPlugin() {
121
155
  const positionToolbar = () => {
122
156
  const toolbar2 = toolbarRef.current;
123
157
  if (!toolbar2) return;
158
+ applyThemeVars(toolbar2);
124
159
  const nativeSelection = window.getSelection();
125
160
  if (!nativeSelection || nativeSelection.rangeCount === 0) {
126
161
  setVisible(false);
@@ -135,7 +170,7 @@ function FloatingToolbarPlugin() {
135
170
  toolbar2.style.left = `${pos.left}px`;
136
171
  };
137
172
  requestAnimationFrame(positionToolbar);
138
- }, [visible, state]);
173
+ }, [applyThemeVars, visible, state]);
139
174
  const handleFormat = useCallback(
140
175
  (format) => {
141
176
  editor.dispatchCommand(FORMAT_TEXT_COMMAND, format);
@@ -172,12 +207,13 @@ function FloatingToolbarPlugin() {
172
207
  [editor]
173
208
  );
174
209
  if (!visible) return null;
210
+ const toolbarClassName = portalClassName ? `${toolbar} ${portalClassName}` : toolbar;
175
211
  return createPortal(
176
212
  /* @__PURE__ */ jsxs(
177
213
  "div",
178
214
  {
179
215
  ref: toolbarRef,
180
- className: toolbar,
216
+ className: toolbarClassName,
181
217
  role: "toolbar",
182
218
  "aria-label": "Text formatting",
183
219
  style: { position: "absolute", zIndex: 50 },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haklex/rich-plugin-floating-toolbar",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "Floating toolbar plugin",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -16,8 +16,8 @@
16
16
  "dist"
17
17
  ],
18
18
  "dependencies": {
19
- "@haklex/rich-editor-ui": "0.0.7",
20
- "@haklex/rich-style-token": "0.0.7"
19
+ "@haklex/rich-editor-ui": "0.0.8",
20
+ "@haklex/rich-style-token": "0.0.8"
21
21
  },
22
22
  "devDependencies": {
23
23
  "@lexical/link": "^0.40.0",