@flozy/editor 9.2.7 → 9.2.9

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,7 +1,7 @@
1
1
  /* eslint-disable no-unused-vars */
2
2
  import React, { useRef, useCallback, useEffect, useMemo, useState, forwardRef, useImperativeHandle, createContext } from "react";
3
3
  import PropTypes from "prop-types";
4
- import { createEditor, Range, Transforms } from "slate";
4
+ import { createEditor, Editor, Range, Transforms } from "slate";
5
5
  import { Slate, Editable, ReactEditor } from "slate-react";
6
6
  import { useDebouncedCallback } from "use-debounce";
7
7
  import { getMarked, getBlock } from "./utils/SlateUtilityFunctions";
@@ -42,6 +42,7 @@ import "animate.css";
42
42
  import FontLoader from "./common/FontLoader/FontLoader";
43
43
  import { ThemeAIIcon, ThemePaintIcon } from "./assets/svg/ThemeIcons";
44
44
  import { CustomDialogComponent } from "./common/CustomDialog";
45
+ import { extractTextWithPath, replaceTextPath } from "./helper/textIndeces.js";
45
46
  import { jsx as _jsx } from "react/jsx-runtime";
46
47
  import { jsxs as _jsxs } from "react/jsx-runtime";
47
48
  export const ThemeContext = /*#__PURE__*/createContext(null);
@@ -86,6 +87,7 @@ const updateTopBanner = (content = [], setTopBanner) => {
86
87
  return firstNode?.type === "topbanner" ? firstNode : null;
87
88
  });
88
89
  };
90
+ const dummyTranslation = () => {};
89
91
  const CommonEditor = /*#__PURE__*/forwardRef((props, ref) => {
90
92
  const {
91
93
  id,
@@ -136,7 +138,7 @@ const CommonEditor = /*#__PURE__*/forwardRef((props, ref) => {
136
138
  hideTools = [],
137
139
  translationMock
138
140
  } = otherProps || {};
139
- const translationFn = translation || translationMock;
141
+ const translationFn = translation || translationMock || dummyTranslation;
140
142
  const editor = useMemo(() => {
141
143
  if (collaborativeEditor) return collaborativeEditor;
142
144
  const editor = createEditor();
@@ -551,6 +553,50 @@ const CommonEditor = /*#__PURE__*/forwardRef((props, ref) => {
551
553
  console.log("handleCursorScroll", err);
552
554
  }
553
555
  };
556
+ window.getTextIndeces = () => {
557
+ try {
558
+ const textWithPaths = extractTextWithPath(editor.children, []);
559
+ return textWithPaths;
560
+ } catch (err) {
561
+ console.log(err);
562
+ }
563
+ };
564
+ window.replaceTextIndeces = (path_data = []) => {
565
+ try {
566
+ const replacedJson = replaceTextPath(JSON.parse(JSON.stringify(editor.children)), path_data);
567
+
568
+ // loop delete all
569
+ editor.children.forEach(() => {
570
+ Transforms.delete(editor, {
571
+ at: [0]
572
+ });
573
+ });
574
+
575
+ // Insert new nodes
576
+ Transforms.insertNodes(editor, replacedJson, {
577
+ at: [0]
578
+ });
579
+ } catch (err) {
580
+ console.log(err);
581
+ }
582
+ };
583
+ window.replaceEditorContent = (fragments = []) => {
584
+ try {
585
+ // loop delete all
586
+ editor.children.forEach(() => {
587
+ Transforms.delete(editor, {
588
+ at: [0]
589
+ });
590
+ });
591
+
592
+ // Insert new nodes
593
+ Transforms.insertNodes(editor, fragments, {
594
+ at: [0]
595
+ });
596
+ } catch (err) {
597
+ console.log(err);
598
+ }
599
+ };
554
600
  return /*#__PURE__*/_jsxs(EditorProvider, {
555
601
  theme: theme,
556
602
  editor: editor,
@@ -1,5 +1,5 @@
1
1
  import React, { useRef, useState } from "react";
2
- import { Node, Transforms } from "slate";
2
+ import { Node, Range, Transforms } from "slate";
3
3
  import { ReactEditor, useSelected, useSlateStatic } from "slate-react";
4
4
  import { Box, IconButton, Popper, Tooltip } from "@mui/material";
5
5
  import OpenInNewIcon from "@mui/icons-material/OpenInNew";
@@ -25,7 +25,11 @@ const Toolbar = props => {
25
25
  const btnProps = handleLinkType(urlPath, linkType, true, showInNewTab === "_blank");
26
26
  const navType = getLinkType(linkType, urlPath);
27
27
  const hideOpenLink = navType === "page" || !navType;
28
- return selected ? /*#__PURE__*/_jsx(Popper, {
28
+
29
+ // To avoid mupltiple link setting pop up opens [Range used]
30
+ const renageSelected = editor.selection && !Range.isCollapsed(editor.selection);
31
+ const openLinkOptions = !renageSelected && selected;
32
+ return openLinkOptions ? /*#__PURE__*/_jsx(Popper, {
29
33
  anchorEl: linkRef?.current,
30
34
  open: true,
31
35
  placement: "top-start",
@@ -154,7 +154,10 @@ const ELEMENTS_LIST = [{
154
154
  icon: /*#__PURE__*/_jsx(Icon, {
155
155
  icon: "image"
156
156
  }),
157
- onInsert: editor => insertDefaultEmbed(editor, "image")
157
+ onInsert: editor => {
158
+ Transforms.delete(editor, editor.selection);
159
+ insertDefaultEmbed(editor, "image");
160
+ }
158
161
  }, {
159
162
  name: "Video",
160
163
  desc: "",
@@ -163,9 +166,12 @@ const ELEMENTS_LIST = [{
163
166
  icon: /*#__PURE__*/_jsx(Icon, {
164
167
  icon: "video"
165
168
  }),
166
- onInsert: editor => insertDefaultEmbed(editor, "video", "", {
167
- aspectRatio: "16 / 9"
168
- })
169
+ onInsert: editor => {
170
+ Transforms.delete(editor, editor.selection);
171
+ insertDefaultEmbed(editor, "video", "", {
172
+ aspectRatio: "16 / 9"
173
+ });
174
+ }
169
175
  }, {
170
176
  name: "Embed",
171
177
  desc: "",
@@ -174,7 +180,10 @@ const ELEMENTS_LIST = [{
174
180
  icon: /*#__PURE__*/_jsx(Icon, {
175
181
  icon: "embed"
176
182
  }),
177
- onInsert: editor => insertDefaultEmbed(editor, "embed", "")
183
+ onInsert: editor => {
184
+ Transforms.delete(editor, editor.selection);
185
+ insertDefaultEmbed(editor, "embed", "");
186
+ }
178
187
  }, {
179
188
  name: "Calendly",
180
189
  desc: "",
@@ -183,7 +192,10 @@ const ELEMENTS_LIST = [{
183
192
  icon: /*#__PURE__*/_jsx(Icon, {
184
193
  icon: "calenderNewIcon"
185
194
  }),
186
- onInsert: editor => insertDefaultEmbed(editor, "calendly", "")
195
+ onInsert: editor => {
196
+ Transforms.delete(editor, editor.selection);
197
+ insertDefaultEmbed(editor, "calendly", "");
198
+ }
187
199
  }, {
188
200
  name: "Table",
189
201
  group: "Elements",
@@ -0,0 +1,58 @@
1
+ export function extractTextWithPath(data, path = []) {
2
+ let result = [];
3
+ data.forEach((item, index) => {
4
+ const currentPath = [...path, index];
5
+ if (item.text) {
6
+ result.push({
7
+ path_id: currentPath.join(","),
8
+ text: item.text
9
+ });
10
+ }
11
+ if (item.children) {
12
+ result = result.concat(extractTextWithPath(item.children, currentPath));
13
+ }
14
+ });
15
+ return result;
16
+ }
17
+ export function replaceTextPath(nestedJson, pathIdJson) {
18
+ // Create a map from path_id JSON for quick lookup
19
+ const pathIdMap = new Map(pathIdJson.map(item => [item.path_id, item.text]));
20
+ console.log(pathIdMap);
21
+ function extractTextWithPath(data, path = []) {
22
+ data.forEach((item, index) => {
23
+ const currentPath = [...path, index];
24
+ if (item.text) {
25
+ if (pathIdMap.has(currentPath?.join(","))) {
26
+ item.text = pathIdMap.get(currentPath?.join(","));
27
+ }
28
+ }
29
+ if (item.children) {
30
+ extractTextWithPath(item.children, currentPath);
31
+ }
32
+ });
33
+ }
34
+ extractTextWithPath(nestedJson, []);
35
+ return nestedJson;
36
+ }
37
+
38
+ // export function replaceTextPath(nestedJson, pathIdJson) {
39
+ // // Create a map from path_id JSON for quick lookup
40
+ // const pathIdMap = new Map(
41
+ // pathIdJson.map((item) => [item.path_id, item.text])
42
+ // );
43
+
44
+ // function traverseAndReplace(node) {
45
+ // if (Array.isArray(node)) {
46
+ // node.forEach(traverseAndReplace);
47
+ // } else if (typeof node === "object" && node !== null) {
48
+ // console.log(node);
49
+ // if (node.text && pathIdMap.has(node.text)) {
50
+ // node.text = pathIdMap.get(node.path_id);
51
+ // }
52
+ // Object.values(node).forEach(traverseAndReplace);
53
+ // }
54
+ // }
55
+
56
+ // traverseAndReplace(nestedJson);
57
+ // return nestedJson;
58
+ // }
@@ -1,5 +1,6 @@
1
1
  import { Transforms } from "slate";
2
2
  import insertNewLine, { insertNewLineAfterCurrentPath } from "./insertNewLine";
3
+ import { getCurrentNodeText } from "./helper";
3
4
  export const insertDefaultEmbed = (editor, type, defaultURL = "", extProps = {}) => {
4
5
  try {
5
6
  const url = defaultURL ? defaultURL : type === "image" ? "" : "";
@@ -51,11 +52,15 @@ export const createEmbedNode = (type, {
51
52
  });
52
53
  export const insertEmbed = (editor, embedData, format) => {
53
54
  try {
55
+ const blockText = getCurrentNodeText(editor);
54
56
  const embed = createEmbedNode(format, embedData);
55
- Transforms.insertNodes(editor, embed, {
56
- at: editor.selection.anchor.path
57
- });
58
- insertNewLine(editor);
57
+ if (!blockText) {
58
+ Transforms.removeNodes(editor, {
59
+ at: editor?.selection
60
+ });
61
+ insertNewLine(editor);
62
+ }
63
+ Transforms.insertNodes(editor, embed);
59
64
  insertNewLineAfterCurrentPath(editor);
60
65
  } catch (err) {
61
66
  console.log(err);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flozy/editor",
3
- "version": "9.2.7",
3
+ "version": "9.2.9",
4
4
  "description": "An Editor for flozy app brain",
5
5
  "files": [
6
6
  "dist"