@cyberyozh/text-editor 0.1.7 → 0.1.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.
Files changed (36) hide show
  1. package/dist/editor.css +12 -2
  2. package/dist/editor.css.map +1 -1
  3. package/dist/editor.d.mts +1 -0
  4. package/dist/editor.d.ts +1 -0
  5. package/dist/editor.js +202 -139
  6. package/dist/editor.js.map +1 -1
  7. package/dist/editor.mjs +200 -137
  8. package/dist/editor.mjs.map +1 -1
  9. package/dist/render.js +76 -11
  10. package/dist/render.js.map +1 -1
  11. package/dist/render.mjs +76 -11
  12. package/dist/render.mjs.map +1 -1
  13. package/dist/styles/index.css +12 -2
  14. package/package.json +2 -4
  15. package/src/editor/controls/bubble-menu-content.tsx +4 -8
  16. package/src/editor/controls/callout-menu.tsx +2 -0
  17. package/src/editor/controls/drag-handle.tsx +6 -1
  18. package/src/editor/dialogs/add-link-dialog/add-link-dialog.css +12 -4
  19. package/src/editor/dialogs/add-link-dialog/add-link-dialog.tsx +65 -28
  20. package/src/editor/dialogs/image-dialog/image-dialog.tsx +1 -1
  21. package/src/editor/editor.tsx +19 -12
  22. package/src/editor/handlers/dialog-handlers.ts +87 -156
  23. package/src/editor/handlers/index.ts +1 -2
  24. package/src/extensions/base/compatibility.ts +52 -0
  25. package/src/extensions/base/figure.ts +7 -0
  26. package/src/extensions/base/link.ts +3 -3
  27. package/src/extensions/views/components/table.tsx +23 -4
  28. package/src/extensions/views/node-views/figure.tsx +1 -1
  29. package/src/render/static-renderer-config.tsx +1 -17
  30. package/src/styles/editor/base.css +1 -1
  31. package/src/styles/extensions/callout.css +1 -1
  32. package/src/translations/en.json +5 -1
  33. package/src/translations/ru.json +5 -1
  34. package/src/types/image-handler.ts +2 -0
  35. package/src/types/link.ts +9 -0
  36. package/src/editor/handlers/create-image-upload-handler.ts +0 -24
@@ -1,6 +1,8 @@
1
1
  import type { Editor } from "@tiptap/react";
2
+ import { TextSelection } from "@tiptap/pm/state";
2
3
 
3
4
  import type { FigureAttrs, TiptapImageAttrs } from "../../types";
5
+ import { LinkDialogContextProps } from "@/types/link";
4
6
 
5
7
  export const createHandleImageAdd = (
6
8
  editor: Editor | null,
@@ -11,40 +13,26 @@ export const createHandleImageAdd = (
11
13
  if (!editor) return;
12
14
 
13
15
  const { state } = editor;
14
- const { $from } = state.selection;
15
- const text = $from.parent.textContent;
16
+ const insertPos = savedPos ?? state.selection.from;
17
+
18
+ editor
19
+ .chain()
20
+ .focus()
21
+ .command(({ tr, state }) => {
22
+ const $pos = state.doc.resolve(insertPos);
23
+ const imageNode = state.schema.nodes.image.create({ ...attrs });
24
+
25
+ if ($pos.parent.type.name === "paragraph" && $pos.parent.textContent === "") {
26
+ tr.replaceWith($pos.before(), $pos.after(), imageNode);
27
+ tr.setSelection(TextSelection.near(tr.doc.resolve($pos.before())));
28
+ } else {
29
+ tr.insert(insertPos, imageNode);
30
+ tr.setSelection(TextSelection.near(tr.doc.resolve(insertPos)));
31
+ }
32
+ return true;
33
+ })
34
+ .run();
16
35
 
17
- if (text.lastIndexOf("/") !== -1) {
18
- editor
19
- .chain()
20
- .focus()
21
- .command(({ tr }) => {
22
- const node = editor.schema.nodes.image.create({ ...attrs });
23
- tr.replaceWith($from.before(), $from.after(), node);
24
- return true;
25
- })
26
- .scrollIntoView()
27
- .run();
28
- } else {
29
- const insertPos = savedPos ?? state.selection.from;
30
-
31
- editor
32
- .chain()
33
- .focus()
34
- .command(({ tr, state }) => {
35
- const $pos = state.doc.resolve(insertPos);
36
- const figureNode = state.schema.nodes.figure.create({ ...attrs });
37
-
38
- if ($pos.parent.type.name === "paragraph" && $pos.parent.textContent === "") {
39
- tr.replaceWith($pos.before(), $pos.after(), figureNode);
40
- } else {
41
- tr.insert(insertPos, figureNode);
42
- }
43
- return true;
44
- })
45
- .scrollIntoView()
46
- .run();
47
- }
48
36
  closeDialogCommand();
49
37
  };
50
38
  };
@@ -71,43 +59,26 @@ export const createHandleVideoAdd = (
71
59
  if (!editor) return;
72
60
 
73
61
  const { state } = editor;
74
- const { $from } = state.selection;
75
- const text = $from.parent.textContent;
62
+ const insertPos = savedPos ?? state.selection.from;
63
+
64
+ editor
65
+ .chain()
66
+ .focus()
67
+ .command(({ tr, state }) => {
68
+ const $pos = state.doc.resolve(insertPos);
69
+ const videoNode = state.schema.nodes.video.create({ src: url });
70
+
71
+ if ($pos.parent.type.name === "paragraph" && $pos.parent.textContent === "") {
72
+ tr.replaceWith($pos.before(), $pos.after(), videoNode);
73
+ tr.setSelection(TextSelection.near(tr.doc.resolve($pos.before())));
74
+ } else {
75
+ tr.insert(insertPos, videoNode);
76
+ tr.setSelection(TextSelection.near(tr.doc.resolve(insertPos)));
77
+ }
78
+ return true;
79
+ })
80
+ .run();
76
81
 
77
- if (text.lastIndexOf("/") !== -1) {
78
- editor
79
- .chain()
80
- .focus()
81
- .command(({ tr }) => {
82
- tr.replaceWith(
83
- $from.before(),
84
- $from.after(),
85
- editor.schema.nodes.video.create({ src: url })
86
- );
87
- return true;
88
- })
89
- .scrollIntoView()
90
- .run();
91
- } else {
92
- const insertPos = savedPos ?? state.selection.from;
93
-
94
- editor
95
- .chain()
96
- .focus()
97
- .command(({ tr, state }) => {
98
- const $pos = state.doc.resolve(insertPos);
99
- const videoNode = state.schema.nodes.video.create({ src: url });
100
-
101
- if ($pos.parent.type.name === "paragraph" && $pos.parent.textContent === "") {
102
- tr.replaceWith($pos.before(), $pos.after(), videoNode);
103
- } else {
104
- tr.insert(insertPos, videoNode);
105
- }
106
- return true;
107
- })
108
- .scrollIntoView()
109
- .run();
110
- }
111
82
  closeDialogCommand();
112
83
  };
113
84
  };
@@ -115,62 +86,43 @@ export const createHandleVideoAdd = (
115
86
  export const createHandleLinkAdd = (
116
87
  editor: Editor | null,
117
88
  closeDialogCommand: () => void,
89
+ linkParams: LinkDialogContextProps,
118
90
  savedPos?: number
119
91
  ) => {
120
- return (title: string, url: string) => {
121
- if (!editor) return;
122
-
123
- const hasSelection = !editor.state.selection.empty;
124
-
125
- if (hasSelection) {
126
- editor
127
- .chain()
128
- .focus()
129
- .command(({ tr, state }) => {
130
- const { from, to } = state.selection;
131
- tr.insertText(title, from, to);
132
- tr.addMark(from, from + title.length, editor.schema.marks.link.create({ href: url }));
133
- return true;
134
- })
135
- .run();
136
- } else {
137
- const { state } = editor;
138
- const { $from } = state.selection;
139
- const text = $from.parent.textContent;
140
- const nodeStart = $from.start();
141
- const beforeCursor = text.substring(0, $from.pos - nodeStart);
142
- const slashIndex = beforeCursor.lastIndexOf("/");
143
-
144
- if (slashIndex !== -1) {
145
- editor
146
- .chain()
147
- .focus()
148
- .command(({ tr }) => {
149
- tr.delete(nodeStart + slashIndex, $from.pos);
150
- return true;
151
- })
152
- .extendMarkRange("link")
153
- .setLink({ href: url })
154
- .insertContent(title)
155
- .scrollIntoView()
156
- .run();
157
- } else {
158
- const insertPos = savedPos ?? state.selection.from;
159
- editor
160
- .chain()
161
- .focus()
162
- .command(({ tr }) => {
163
- const linkMark = editor.schema.marks.link.create({ href: url });
164
- const textNode = editor.schema.text(title, [linkMark]);
165
- tr.insert(insertPos, textNode);
166
- return true;
167
- })
168
- .scrollIntoView()
169
- .run();
170
- }
171
- }
172
- closeDialogCommand();
173
- };
92
+ if (!editor) return;
93
+
94
+ const hasSelection = !editor.state.selection.empty;
95
+ const titleLength = linkParams.title?.length ?? 0;
96
+
97
+ if (hasSelection) {
98
+ editor
99
+ .chain()
100
+ .focus()
101
+ .command(({ tr, state }) => {
102
+ const { from, to } = state.selection;
103
+ tr.insertText(linkParams.title ?? "", from, to);
104
+ tr.addMark(from, from + titleLength, editor.schema.marks.link.create({ ...linkParams }));
105
+ return true;
106
+ })
107
+ .run();
108
+ } else {
109
+ const { state } = editor;
110
+ const insertPos = savedPos ?? state.selection.from;
111
+ const titleLength = linkParams.title?.length ?? 0;
112
+
113
+ editor
114
+ .chain()
115
+ .focus()
116
+ .command(({ tr }) => {
117
+ const linkMark = editor.schema.marks.link.create({ ...linkParams });
118
+ const textNode = editor.schema.text(linkParams.title ?? "", [linkMark]);
119
+ tr.insert(insertPos, textNode);
120
+ tr.setSelection(TextSelection.create(tr.doc, insertPos, insertPos + titleLength));
121
+ return true;
122
+ })
123
+ .run();
124
+ }
125
+ closeDialogCommand();
174
126
  };
175
127
 
176
128
  export const createHandleKatexAdd = (
@@ -185,39 +137,18 @@ export const createHandleKatexAdd = (
185
137
  editor.chain().setNodeSelection(pos).updateInlineMath({ latex }).focus().run();
186
138
  } else {
187
139
  const { state } = editor;
188
- const { $from } = state.selection;
189
- const text = $from.parent.textContent;
190
- const nodeStart = $from.start();
191
- const beforeCursor = text.substring(0, $from.pos - nodeStart);
192
- const slashIndex = beforeCursor.lastIndexOf("/");
193
-
194
- if (slashIndex !== -1) {
195
- const deleteFrom = nodeStart + slashIndex;
196
- const deleteTo = $from.pos;
197
-
198
- editor
199
- .chain()
200
- .focus()
201
- .command(({ tr, state }) => {
202
- const inlineMathNode = state.schema.nodes.inlineMath.create({ latex });
203
- tr.replaceRangeWith(deleteFrom, deleteTo, inlineMathNode);
204
- return true;
205
- })
206
- .scrollIntoView()
207
- .run();
208
- } else {
209
- const insertPos = savedPos ?? state.selection.from;
210
- editor
211
- .chain()
212
- .focus()
213
- .command(({ tr, state }) => {
214
- const inlineMathNode = state.schema.nodes.inlineMath.create({ latex });
215
- tr.insert(insertPos, inlineMathNode);
216
- return true;
217
- })
218
- .scrollIntoView()
219
- .run();
220
- }
140
+ const insertPos = savedPos ?? state.selection.from;
141
+
142
+ editor
143
+ .chain()
144
+ .focus()
145
+ .command(({ tr, state }) => {
146
+ const inlineMathNode = state.schema.nodes.inlineMath.create({ latex });
147
+ tr.insert(insertPos, inlineMathNode);
148
+ tr.setSelection(TextSelection.near(tr.doc.resolve(insertPos)));
149
+ return true;
150
+ })
151
+ .run();
221
152
  }
222
153
 
223
154
  closeDialogCommand();
@@ -1,8 +1,7 @@
1
- export { createImageUploadHandler } from './create-image-upload-handler';
2
1
  export {
3
2
  createHandleImageAdd,
4
3
  createHandleKatexAdd,
5
4
  createHandleLinkAdd,
6
5
  createHandleVideoAdd,
7
6
  createHandleImageEdit,
8
- } from './dialog-handlers';
7
+ } from "./dialog-handlers";
@@ -24,6 +24,32 @@ export const BaseLegacyBookAlert = Node.create({
24
24
  type: {
25
25
  default: "error",
26
26
  },
27
+ bgColor: {
28
+ default: null,
29
+ parseHTML: (element) => {
30
+ if (typeof element === "string") return null;
31
+ return element.getAttribute("data-bg-color");
32
+ },
33
+ renderHTML: (attributes) => {
34
+ if (!attributes.bgColor) return {};
35
+ return {
36
+ "data-bg-color": attributes.bgColor,
37
+ };
38
+ },
39
+ },
40
+ emoji: {
41
+ default: null,
42
+ parseHTML: (element) => {
43
+ if (typeof element === "string") return null;
44
+ return element.getAttribute("data-emoji");
45
+ },
46
+ renderHTML: (attributes) => {
47
+ if (!attributes.emoji) return {};
48
+ return {
49
+ "data-emoji": attributes.emoji,
50
+ };
51
+ },
52
+ },
27
53
  };
28
54
  },
29
55
 
@@ -65,6 +91,32 @@ export const BaseLegacyBookTip = Node.create({
65
91
  type: {
66
92
  default: "info",
67
93
  },
94
+ bgColor: {
95
+ default: null,
96
+ parseHTML: (element) => {
97
+ if (typeof element === "string") return null;
98
+ return element.getAttribute("data-bg-color");
99
+ },
100
+ renderHTML: (attributes) => {
101
+ if (!attributes.bgColor) return {};
102
+ return {
103
+ "data-bg-color": attributes.bgColor,
104
+ };
105
+ },
106
+ },
107
+ emoji: {
108
+ default: null,
109
+ parseHTML: (element) => {
110
+ if (typeof element === "string") return null;
111
+ return element.getAttribute("data-emoji");
112
+ },
113
+ renderHTML: (attributes) => {
114
+ if (!attributes.emoji) return {};
115
+ return {
116
+ "data-emoji": attributes.emoji,
117
+ };
118
+ },
119
+ },
68
120
  };
69
121
  },
70
122
 
@@ -52,6 +52,13 @@ export const BaseFigure = Image.extend<FigureOptions>({
52
52
  default: null,
53
53
  parseHTML: (element) => element.querySelector("figcaption")?.textContent || null,
54
54
  },
55
+ storageKey: {
56
+ default: null,
57
+ parseHTML: (element) => element.getAttribute("data-storage-key"),
58
+ renderHTML: (attributes) => ({
59
+ "data-storage-key": attributes.storageKey,
60
+ }),
61
+ },
55
62
  };
56
63
  },
57
64
 
@@ -1,4 +1,4 @@
1
- import Link from '@tiptap/extension-link';
1
+ import Link from "@tiptap/extension-link";
2
2
 
3
3
  export const LinkExtension = Link.extend({
4
4
  addOptions() {
@@ -7,12 +7,12 @@ export const LinkExtension = Link.extend({
7
7
  openOnClick: false,
8
8
  linkOnPaste: true,
9
9
  autolink: true,
10
- protocols: ['http', 'https', 'mailto', 'tel'],
10
+ protocols: ["http", "https", "mailto", "tel"],
11
11
  };
12
12
  },
13
13
  }).configure({
14
14
  openOnClick: false,
15
15
  linkOnPaste: true,
16
16
  autolink: true,
17
- protocols: ['http', 'https', 'mailto', 'tel'],
17
+ protocols: ["http", "https", "mailto", "tel"],
18
18
  });
@@ -2,15 +2,31 @@ import type { ReactNode } from "react";
2
2
 
3
3
  interface TableProps {
4
4
  children: ReactNode;
5
- colWidths?: number[];
6
5
  }
7
6
 
8
7
  /**
9
8
  * Table component for static rendering
10
9
  * Applies column widths from Tiptap table cells
11
10
  */
12
- export function Table({ children, colWidths = [] }: TableProps) {
13
- const tableWidth = colWidths.length > 0 ? colWidths.reduce((total, width) => total + width, 0) : null;
11
+ export function Table({ children }: TableProps) {
12
+ let colWidths: number[] = [];
13
+
14
+ if (Array.isArray(children) && children.length > 0) {
15
+ const firstRow = children[0];
16
+
17
+ if (firstRow?.props?.children) {
18
+ const cells = Array.isArray(firstRow.props.children)
19
+ ? firstRow.props.children
20
+ : [firstRow.props.children];
21
+
22
+ colWidths = cells
23
+ .map((cell: any) => cell?.props?.node?.attrs?.colwidth?.[0])
24
+ .filter((width: number | undefined) => width !== undefined);
25
+ }
26
+ }
27
+
28
+ const tableWidth =
29
+ colWidths.length > 0 ? colWidths.reduce((total, width) => total + width, 0) : null;
14
30
 
15
31
  return (
16
32
  <div className="tableWrapper">
@@ -18,7 +34,10 @@ export function Table({ children, colWidths = [] }: TableProps) {
18
34
  {colWidths.length > 0 && (
19
35
  <colgroup>
20
36
  {colWidths.map((width, index) => (
21
- <col key={index} style={{ width: `${width}px` }} />
37
+ <col
38
+ key={index}
39
+ style={{ width: `${width}px` }}
40
+ />
22
41
  ))}
23
42
  </colgroup>
24
43
  )}
@@ -6,7 +6,7 @@ import { FigureAttrs } from "../../../types";
6
6
  import { Figure } from "../components";
7
7
 
8
8
  export const FigureView = ({ node, getPos, editor }: NodeViewProps) => {
9
- const { fileId, src, alt, title, caption, width, height } = node.attrs as FigureAttrs;
9
+ const { fileId, src, alt, title, caption, width, height, storageKey } = node.attrs as FigureAttrs;
10
10
 
11
11
  const imageExtension = editor.extensionManager.extensions.find((ext) => ext.name === "image");
12
12
  const getImageById = imageExtension?.options.getImageById;
@@ -96,23 +96,7 @@ export const getNodeMapping = (getImageById?: (fileId: string) => string | null)
96
96
  return <Video src={node.attrs.src} />;
97
97
  },
98
98
  table({ children }: any) {
99
- let colWidths: number[] = [];
100
-
101
- if (Array.isArray(children) && children.length > 0) {
102
- const firstRow = children[0];
103
-
104
- if (firstRow?.props?.children) {
105
- const cells = Array.isArray(firstRow.props.children)
106
- ? firstRow.props.children
107
- : [firstRow.props.children];
108
-
109
- colWidths = cells
110
- .map((cell: any) => cell?.props?.node?.attrs?.colwidth?.[0])
111
- .filter((width: number | undefined) => width !== undefined);
112
- }
113
- }
114
-
115
- return <Table colWidths={colWidths}>{children}</Table>;
99
+ return <Table>{children}</Table>;
116
100
  },
117
101
  figure({ node }: any) {
118
102
  return (
@@ -8,7 +8,7 @@
8
8
  font-family: var(--tiptap-font-family);
9
9
  }
10
10
 
11
- .tiptap-content-wrapper:focus {
11
+ .tiptap-content-wrapper:focus-within {
12
12
  border-color: var(--tiptap-color-primary-light);
13
13
  outline: none;
14
14
  }
@@ -105,7 +105,7 @@
105
105
  flex: 1;
106
106
  min-width: 0;
107
107
  font-size: var(--tiptap-font-size);
108
- line-height: 1;
108
+ line-height: 140%;
109
109
  }
110
110
 
111
111
  .callout-content {
@@ -32,7 +32,11 @@
32
32
  "invalidUrl": "Invalid URL",
33
33
  "emptyUrl": "Enter link URL",
34
34
  "addLinkTitle": "Add link",
35
- "removeLinkTitle": "Remove link"
35
+ "removeLinkTitle": "Remove link",
36
+ "openInNewTab": "Open in new tab",
37
+ "openInNewTabHelp": "Link will open in a new browser window or tab",
38
+ "addNofollow": "Add rel=\"nofollow\"",
39
+ "addNofollowHelp": "Tells search engines not to count this link when calculating page rankings and indexing. Recommended for all external links."
36
40
  },
37
41
  "lists": {
38
42
  "bulletList": "Bullet list",
@@ -32,7 +32,11 @@
32
32
  "invalidUrl": "Неверный URL",
33
33
  "emptyUrl": "Введите URL ссылки",
34
34
  "addLinkTitle": "Добавить ссылку",
35
- "removeLinkTitle": "Удалить ссылку"
35
+ "removeLinkTitle": "Удалить ссылку",
36
+ "openInNewTab": "Открыть в новой вкладке",
37
+ "openInNewTabHelp": "Ссылка откроется в новом окне или вкладке браузера",
38
+ "addNofollow": "Добавить rel=\"nofollow\"",
39
+ "addNofollowHelp": "Указывает поисковым системам не учитывать ссылку при расчете рейтинга и индексации страницы, на которой они размещены. Рекомендуется применять ко всем ссылкам на сторонние сайты."
36
40
  },
37
41
  "lists": {
38
42
  "bulletList": "Ненумерованный список",
@@ -1,6 +1,7 @@
1
1
  export interface ImageUploadResult {
2
2
  fileId?: string;
3
3
  fileUrl?: string;
4
+ storageKey?: string;
4
5
  }
5
6
 
6
7
  export interface ImageUploadHandler {
@@ -24,6 +25,7 @@ export interface FigureAttrs {
24
25
  caption?: string;
25
26
  width?: number | null;
26
27
  height?: number | null;
28
+ storageKey?: string;
27
29
  }
28
30
 
29
31
  export interface TiptapImageAttrs extends FigureAttrs {}
@@ -0,0 +1,9 @@
1
+ export interface LinkDialogContextProps extends LinkAttrsType {
2
+ title?: string;
3
+ }
4
+
5
+ export interface LinkAttrsType {
6
+ href: string;
7
+ target: string;
8
+ rel: string | null;
9
+ }
@@ -1,24 +0,0 @@
1
- import { ImageUploadHandler } from "../../types";
2
-
3
- /**
4
- * Creates an image upload handler that wraps a custom upload action
5
- * Supports two patterns:
6
- * 1. Backend with fileId: { fileId: "file-123" }
7
- * 2. Direct CDN: { fileUrl: "https://cdn.com/image.jpg" }
8
- */
9
- export function createImageUploadHandler(
10
- uploadAction: (file: File) => Promise<{ data?: { fileId?: string; fileUrl?: string } }>
11
- ): ImageUploadHandler {
12
- return async (file: File) => {
13
- const { data } = await uploadAction(file);
14
-
15
- if (!data?.fileId && !data?.fileUrl) {
16
- throw new Error("Failed to upload file: must return fileId or fileUrl");
17
- }
18
-
19
- return {
20
- fileId: data.fileId,
21
- fileUrl: data.fileUrl,
22
- };
23
- };
24
- }