@flozy/editor 10.5.8 → 10.5.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,6 +1,5 @@
1
- import { Editor, Range, Transforms } from "slate";
1
+ import { Editor, Element, Transforms } from "slate";
2
2
  import insertNewLine from "./insertNewLine";
3
- import { getCurrentNodeText, getSelectedText } from "./helper";
4
3
  import { isMobileWindow } from "../helper";
5
4
  const focusAccordion = (editor, upPath) => {
6
5
  Transforms.select(editor, {
@@ -8,77 +7,52 @@ const focusAccordion = (editor, upPath) => {
8
7
  offset: 0
9
8
  });
10
9
  };
11
- function isSelectionInSingleNode(editor) {
12
- const {
13
- anchor,
14
- focus
15
- } = editor.selection;
16
- return Editor.path(editor, anchor).toString() === Editor.path(editor, focus).toString();
17
- }
18
10
  export const insertAccordion = (editor, path) => {
19
11
  try {
20
12
  const {
21
13
  selection
22
14
  } = editor;
23
- const isHavingSelection = selection && !Range.isCollapsed(selection);
24
- const selectedText = isHavingSelection ? getSelectedText(editor) : "";
25
- const isSingleNodeSelected = isHavingSelection && isSelectionInSingleNode(editor);
15
+ if (selection) {
16
+ const selectedNodes = Array.from(Editor.nodes(editor, {
17
+ at: selection,
18
+ match: n => Element.isElement(n),
19
+ mode: "lowest" // use 'lowest' to get individual blocks
20
+ }));
26
21
 
27
- // Fully select the heading text and convert it to an accordion.
28
- // An empty heading is inserted just before the newly created accordion.
29
- let fullySelected = false;
30
- if (isSingleNodeSelected) {
31
- const currentNodeText = selectedText ? getCurrentNodeText(editor) : "";
32
- fullySelected = selectedText?.length && currentNodeText.length === selectedText?.length;
33
- }
34
- const accordion = {
35
- type: "accordion",
36
- children: [{
37
- type: "accordion-summary",
38
- children: [{
39
- type: "paragraph",
40
- children: [{
41
- text: selectedText || ""
42
- }]
43
- }]
44
- }, {
45
- type: "accordion-details",
46
- children: [{
47
- type: "paragraph",
22
+ for (const [node, path] of selectedNodes) {
23
+ const accordion = {
24
+ type: "accordion",
48
25
  children: [{
49
- text: ""
26
+ type: "accordion-summary",
27
+ children: [node]
28
+ }, {
29
+ type: "accordion-details",
30
+ children: [{
31
+ type: "paragraph",
32
+ children: [{
33
+ text: ""
34
+ }]
35
+ }]
50
36
  }]
51
- }]
52
- }]
53
- };
54
- const props = path ? {
55
- at: path,
56
- select: true
57
- } : {
58
- select: true
59
- };
60
- if (fullySelected) {
61
- const insertPath = editor.selection.anchor.path;
62
- Transforms.removeNodes(editor, {
63
- at: insertPath
64
- });
65
- props.at = insertPath;
66
- }
67
- Transforms.insertNodes(editor, accordion, props);
68
- const curPath = [...editor?.selection?.anchor?.path];
69
- const upPath = [...curPath];
70
- // get title index
71
- const summaryIndex = upPath.length - 3;
72
- upPath[summaryIndex] = upPath[summaryIndex] === 0 ? 0 : upPath[summaryIndex] - 1;
73
-
74
- // select accordion title by default
75
- if (isMobileWindow()) {
76
- // timeout to resolve focus issue in mobile
77
- setTimeout(() => focusAccordion(editor, upPath), 0);
78
- } else {
79
- focusAccordion(editor, upPath);
37
+ };
38
+ Transforms.removeNodes(editor, {
39
+ at: path
40
+ });
41
+ Transforms.insertNodes(editor, accordion, {
42
+ at: path
43
+ });
44
+ }
45
+ const lastNode = selectedNodes[selectedNodes.length - 1];
46
+ const lastNodePath = lastNode[1];
47
+ const accordionPath = [...lastNodePath, 0, 0, 0];
48
+ if (isMobileWindow()) {
49
+ // timeout to resolve focus issue in mobile
50
+ setTimeout(() => focusAccordion(editor, accordionPath), 0);
51
+ } else {
52
+ focusAccordion(editor, accordionPath);
53
+ }
54
+ insertNewLine(editor);
80
55
  }
81
- insertNewLine(editor);
82
56
  } catch (err) {
83
57
  console.log(err);
84
58
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flozy/editor",
3
- "version": "10.5.8",
3
+ "version": "10.5.9",
4
4
  "description": "An Editor for flozy app brain",
5
5
  "files": [
6
6
  "dist"