@kerebron/editor 0.4.27 → 0.4.29

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 (167) hide show
  1. package/esm/CoreEditor.d.ts +40 -0
  2. package/esm/CoreEditor.d.ts.map +1 -0
  3. package/esm/CoreEditor.js +224 -0
  4. package/esm/CoreEditor.js.map +1 -0
  5. package/esm/DummyEditorView.d.ts +60 -0
  6. package/esm/DummyEditorView.d.ts.map +1 -0
  7. package/esm/DummyEditorView.js +243 -0
  8. package/esm/DummyEditorView.js.map +1 -0
  9. package/esm/Extension.d.ts +31 -0
  10. package/esm/Extension.d.ts.map +1 -0
  11. package/esm/Extension.js +35 -0
  12. package/esm/Extension.js.map +1 -0
  13. package/esm/ExtensionManager.d.ts +32 -0
  14. package/esm/ExtensionManager.d.ts.map +1 -0
  15. package/esm/ExtensionManager.js +223 -0
  16. package/esm/ExtensionManager.js.map +1 -0
  17. package/esm/Mark.d.ts +23 -0
  18. package/esm/Mark.d.ts.map +1 -0
  19. package/esm/Mark.js +28 -0
  20. package/esm/Mark.js.map +1 -0
  21. package/esm/Node.d.ts +32 -0
  22. package/esm/Node.d.ts.map +1 -0
  23. package/esm/Node.js +37 -0
  24. package/esm/Node.js.map +1 -0
  25. package/esm/commands/CommandManager.d.ts +24 -0
  26. package/esm/commands/CommandManager.d.ts.map +1 -0
  27. package/esm/commands/CommandManager.js +101 -0
  28. package/esm/commands/CommandManager.js.map +1 -0
  29. package/esm/commands/baseCommandFactories.d.ts +3 -0
  30. package/esm/commands/baseCommandFactories.d.ts.map +1 -0
  31. package/esm/commands/baseCommandFactories.js +862 -0
  32. package/esm/commands/baseCommandFactories.js.map +1 -0
  33. package/esm/commands/createChainableState.d.ts +3 -0
  34. package/esm/commands/createChainableState.d.ts.map +1 -0
  35. package/esm/commands/createChainableState.js +30 -0
  36. package/esm/commands/createChainableState.js.map +1 -0
  37. package/esm/commands/keyCommandFactories.d.ts +3 -0
  38. package/esm/commands/keyCommandFactories.d.ts.map +1 -0
  39. package/esm/commands/keyCommandFactories.js +11 -0
  40. package/esm/commands/keyCommandFactories.js.map +1 -0
  41. package/esm/commands/mod.d.ts +7 -0
  42. package/esm/commands/mod.d.ts.map +1 -0
  43. package/esm/commands/mod.js +82 -0
  44. package/esm/commands/mod.js.map +1 -0
  45. package/esm/commands/replaceCommandFactories.d.ts +3 -0
  46. package/esm/commands/replaceCommandFactories.d.ts.map +1 -0
  47. package/esm/commands/replaceCommandFactories.js +95 -0
  48. package/esm/commands/replaceCommandFactories.js.map +1 -0
  49. package/esm/commands/types.d.ts +22 -0
  50. package/esm/commands/types.d.ts.map +1 -0
  51. package/esm/commands/types.js +2 -0
  52. package/esm/commands/types.js.map +1 -0
  53. package/esm/mod.d.ts +9 -0
  54. package/esm/mod.d.ts.map +1 -0
  55. package/esm/mod.js +9 -0
  56. package/esm/mod.js.map +1 -0
  57. package/esm/nodeToTreeString.d.ts +10 -0
  58. package/esm/nodeToTreeString.d.ts.map +1 -0
  59. package/esm/nodeToTreeString.js +75 -0
  60. package/esm/nodeToTreeString.js.map +1 -0
  61. package/esm/package.json +3 -0
  62. package/esm/plugins/TrackSelecionPlugin.d.ts +6 -0
  63. package/esm/plugins/TrackSelecionPlugin.d.ts.map +1 -0
  64. package/esm/plugins/TrackSelecionPlugin.js +25 -0
  65. package/esm/plugins/TrackSelecionPlugin.js.map +1 -0
  66. package/esm/plugins/input-rules/InputRulesPlugin.d.ts +25 -0
  67. package/esm/plugins/input-rules/InputRulesPlugin.d.ts.map +1 -0
  68. package/esm/plugins/input-rules/InputRulesPlugin.js +169 -0
  69. package/esm/plugins/input-rules/InputRulesPlugin.js.map +1 -0
  70. package/esm/plugins/input-rules/mod.d.ts +3 -0
  71. package/esm/plugins/input-rules/mod.d.ts.map +1 -0
  72. package/esm/plugins/input-rules/mod.js +3 -0
  73. package/esm/plugins/input-rules/mod.js.map +1 -0
  74. package/esm/plugins/input-rules/rulebuilders.d.ts +6 -0
  75. package/esm/plugins/input-rules/rulebuilders.d.ts.map +1 -0
  76. package/esm/plugins/input-rules/rulebuilders.js +65 -0
  77. package/esm/plugins/input-rules/rulebuilders.js.map +1 -0
  78. package/esm/plugins/keymap/keymap.d.ts +12 -0
  79. package/esm/plugins/keymap/keymap.d.ts.map +1 -0
  80. package/esm/plugins/keymap/keymap.js +126 -0
  81. package/esm/plugins/keymap/keymap.js.map +1 -0
  82. package/esm/plugins/keymap/mod.d.ts +2 -0
  83. package/esm/plugins/keymap/mod.d.ts.map +1 -0
  84. package/esm/plugins/keymap/mod.js +2 -0
  85. package/esm/plugins/keymap/mod.js.map +1 -0
  86. package/esm/plugins/keymap/w3c-keyname.d.ts +4 -0
  87. package/esm/plugins/keymap/w3c-keyname.d.ts.map +1 -0
  88. package/esm/plugins/keymap/w3c-keyname.js +125 -0
  89. package/esm/plugins/keymap/w3c-keyname.js.map +1 -0
  90. package/esm/search/mod.d.ts +3 -0
  91. package/esm/search/mod.d.ts.map +1 -0
  92. package/esm/search/mod.js +3 -0
  93. package/esm/search/mod.js.map +1 -0
  94. package/esm/search/query.d.ts +45 -0
  95. package/esm/search/query.d.ts.map +1 -0
  96. package/esm/search/query.js +335 -0
  97. package/esm/search/query.js.map +1 -0
  98. package/esm/search/search.d.ts +32 -0
  99. package/esm/search/search.d.ts.map +1 -0
  100. package/esm/search/search.js +200 -0
  101. package/esm/search/search.js.map +1 -0
  102. package/esm/types.d.ts +59 -0
  103. package/esm/types.d.ts.map +1 -0
  104. package/esm/types.js +2 -0
  105. package/esm/types.js.map +1 -0
  106. package/esm/ui.d.ts +15 -0
  107. package/esm/ui.d.ts.map +1 -0
  108. package/esm/ui.js +17 -0
  109. package/esm/ui.js.map +1 -0
  110. package/esm/utilities/SmartOutput.d.ts +41 -0
  111. package/esm/utilities/SmartOutput.d.ts.map +1 -0
  112. package/esm/utilities/SmartOutput.js +202 -0
  113. package/esm/utilities/SmartOutput.js.map +1 -0
  114. package/esm/utilities/createNodeFromContent.d.ts +9 -0
  115. package/esm/utilities/createNodeFromContent.d.ts.map +1 -0
  116. package/esm/utilities/createNodeFromContent.js +33 -0
  117. package/esm/utilities/createNodeFromContent.js.map +1 -0
  118. package/esm/utilities/getHtmlAttributes.d.ts +9 -0
  119. package/esm/utilities/getHtmlAttributes.d.ts.map +1 -0
  120. package/esm/utilities/getHtmlAttributes.js +48 -0
  121. package/esm/utilities/getHtmlAttributes.js.map +1 -0
  122. package/esm/utilities/getShadowRoot.d.ts +2 -0
  123. package/esm/utilities/getShadowRoot.d.ts.map +1 -0
  124. package/esm/utilities/getShadowRoot.js +17 -0
  125. package/esm/utilities/getShadowRoot.js.map +1 -0
  126. package/esm/utilities/mod.d.ts +6 -0
  127. package/esm/utilities/mod.d.ts.map +1 -0
  128. package/esm/utilities/mod.js +6 -0
  129. package/esm/utilities/mod.js.map +1 -0
  130. package/esm/utilities/toRawTextResult.d.ts +3 -0
  131. package/esm/utilities/toRawTextResult.d.ts.map +1 -0
  132. package/esm/utilities/toRawTextResult.js +22 -0
  133. package/esm/utilities/toRawTextResult.js.map +1 -0
  134. package/package.json +5 -2
  135. package/src/CoreEditor.ts +277 -0
  136. package/src/DummyEditorView.ts +403 -0
  137. package/src/Extension.ts +63 -0
  138. package/src/ExtensionManager.ts +328 -0
  139. package/src/Mark.ts +47 -0
  140. package/src/Node.ts +66 -0
  141. package/src/commands/CommandManager.ts +145 -0
  142. package/src/commands/baseCommandFactories.ts +1103 -0
  143. package/src/commands/createChainableState.ts +36 -0
  144. package/src/commands/keyCommandFactories.ts +26 -0
  145. package/src/commands/mod.ts +104 -0
  146. package/src/commands/replaceCommandFactories.ts +129 -0
  147. package/src/commands/types.ts +30 -0
  148. package/src/mod.ts +8 -0
  149. package/src/nodeToTreeString.ts +100 -0
  150. package/src/plugins/TrackSelecionPlugin.ts +27 -0
  151. package/src/plugins/input-rules/InputRulesPlugin.ts +242 -0
  152. package/src/plugins/input-rules/mod.ts +2 -0
  153. package/src/plugins/input-rules/rulebuilders.ts +88 -0
  154. package/src/plugins/keymap/keymap.ts +117 -0
  155. package/src/plugins/keymap/mod.ts +1 -0
  156. package/src/plugins/keymap/w3c-keyname.ts +123 -0
  157. package/src/search/mod.ts +2 -0
  158. package/src/search/query.ts +412 -0
  159. package/src/search/search.ts +284 -0
  160. package/src/types.ts +71 -0
  161. package/src/ui.ts +35 -0
  162. package/src/utilities/SmartOutput.ts +284 -0
  163. package/src/utilities/createNodeFromContent.ts +66 -0
  164. package/src/utilities/getHtmlAttributes.ts +68 -0
  165. package/src/utilities/getShadowRoot.ts +18 -0
  166. package/src/utilities/mod.ts +5 -0
  167. package/src/utilities/toRawTextResult.ts +27 -0
package/src/types.ts ADDED
@@ -0,0 +1,71 @@
1
+ import type { Node as ProseMirrorNode, ParseOptions } from 'prosemirror-model';
2
+ import type { Extension } from './Extension.js';
3
+ import type { Mark } from './Mark.js';
4
+ import type { Node } from './Node.js';
5
+
6
+ export type AnyExtension = Extension | Node | Mark;
7
+ export type AnyExtensionOrReq = AnyExtension | {
8
+ requires: Array<AnyExtensionOrReq | string>;
9
+ };
10
+
11
+ export type Content = JSONContent | JSONContent[] | null;
12
+
13
+ export interface EditorConfig {
14
+ element: HTMLElement;
15
+ content: Content;
16
+ parseOptions: ParseOptions;
17
+ extensions: AnyExtensionOrReq[];
18
+ cdnUrl?: string;
19
+ uri?: string;
20
+ languageID?: string;
21
+ topNode?: string;
22
+ readOnly?: boolean;
23
+ }
24
+
25
+ export type JSONContent = {
26
+ type?: string;
27
+ attrs?: Record<string, any>;
28
+ content?: JSONContent[];
29
+ marks?: {
30
+ type: string;
31
+ attrs?: Record<string, any>;
32
+ [key: string]: any;
33
+ }[];
34
+ text?: string;
35
+ [key: string]: any;
36
+ };
37
+
38
+ export type Attribute<T> = {
39
+ fromDom?: (element: HTMLElement) => T;
40
+ default: T;
41
+ toDom?: (node: ProseMirrorNode) => T;
42
+ };
43
+
44
+ export interface TextRange {
45
+ from: number;
46
+ to: number;
47
+ }
48
+
49
+ export interface RawTextMapEntry {
50
+ nodeIdx: number;
51
+ targetRow: number;
52
+ targetCol: number;
53
+ sourceCol?: number;
54
+ targetPos: number;
55
+ }
56
+
57
+ export interface RawTextResult {
58
+ content: string;
59
+ rawTextMap: Array<RawTextMapEntry>;
60
+ }
61
+
62
+ export interface UrlRewriteContext {
63
+ type: 'IMG' | 'A';
64
+ dest: string; // Dest format, eg: kerebron, md, odt
65
+ filesMap?: Record<string, Uint8Array>;
66
+ }
67
+
68
+ export type UrlRewriter = (
69
+ url: string,
70
+ ctx: UrlRewriteContext,
71
+ ) => Promise<string>;
package/src/ui.ts ADDED
@@ -0,0 +1,35 @@
1
+ import type { CoreEditor } from './CoreEditor.js';
2
+
3
+ interface SelectParams {
4
+ anchor: number;
5
+ scrollIntoView: boolean;
6
+ userEvent: string;
7
+ }
8
+
9
+ export interface EditorUi {
10
+ showMessage(msg: string): void;
11
+ showError(err: Error): void;
12
+ focus(): void;
13
+ select(params: SelectParams): void;
14
+ }
15
+
16
+ export function defaultUi(editor: CoreEditor): EditorUi {
17
+ return {
18
+ showMessage(msg: string) {
19
+ globalThis.alert(msg);
20
+ },
21
+ showError(err: Error) {
22
+ globalThis.alert(err.message);
23
+ console.error(err);
24
+ },
25
+ focus() {
26
+ // editor.run.focus();
27
+ },
28
+ select({
29
+ anchor: number,
30
+ scrollIntoView: boolean,
31
+ userEvent: string,
32
+ }) {
33
+ },
34
+ };
35
+ }
@@ -0,0 +1,284 @@
1
+ export interface OutputMeta<K> {
2
+ pos: number;
3
+ rowPos: number;
4
+ colPos: number;
5
+ item?: K;
6
+ }
7
+
8
+ interface Mapping {
9
+ sourceNo: number;
10
+ sourceRowPos: number;
11
+ sourceColPos: number;
12
+ }
13
+
14
+ export interface SourceMap {
15
+ 'version': 3; // 3,
16
+ 'file': string; // "out.js",
17
+ 'sourceRoot': string; // ""
18
+ 'sources': Array<string>; // ["foo.js", "bar.js"],
19
+ 'sourcesContent': Array<string>; // [null, null],
20
+ 'names': Array<string>; // ["src", "maps", "are", "fun"],
21
+ 'mappings': string; // "A,AAAB;;ABCDE;"
22
+ }
23
+
24
+ export class SmartOutput<K> {
25
+ private _rowPos = 0;
26
+ private _colPos = 0;
27
+ private _pos = 0;
28
+
29
+ private chunks: Array<string> = [];
30
+ private metas: Array<OutputMeta<K>> = [];
31
+
32
+ log(text: string, item?: K) {
33
+ if (text.length === 0) {
34
+ return;
35
+ }
36
+
37
+ this.chunks.push(text);
38
+
39
+ this.metas.push({
40
+ pos: this._pos,
41
+ colPos: this._colPos,
42
+ rowPos: this._rowPos,
43
+ item,
44
+ });
45
+
46
+ const lines = text.split('\n');
47
+
48
+ if (lines.length === 1) {
49
+ this._colPos += lines[lines.length - 1].length;
50
+ } else {
51
+ this._rowPos += lines.length - 1;
52
+ this._colPos = lines[lines.length - 1].length;
53
+ }
54
+ this._pos += text.length;
55
+ }
56
+
57
+ getMetas() {
58
+ return this.metas;
59
+ }
60
+
61
+ get chunkPos() {
62
+ return this.chunks.length;
63
+ }
64
+
65
+ rollback(pos: number) {
66
+ this.chunks.splice(pos);
67
+ this.metas.splice(pos);
68
+ }
69
+
70
+ get rowPos() {
71
+ return this._rowPos;
72
+ }
73
+
74
+ get colPos() {
75
+ return this._colPos;
76
+ }
77
+
78
+ get pos() {
79
+ return this._pos;
80
+ }
81
+
82
+ endsWith(text: string) {
83
+ return this.chunks.join('').endsWith(text);
84
+ }
85
+
86
+ toString() {
87
+ return this.chunks.join('');
88
+ }
89
+
90
+ getSourceMap(
91
+ mapper: (
92
+ rowPos: number,
93
+ colPos: number,
94
+ pos: number,
95
+ item?: K,
96
+ ) => Mapping | void,
97
+ ): SourceMap {
98
+ const mappingRows: Array<Array<Array<number>>> = [];
99
+
100
+ let lastRow = -1;
101
+ let lastCol = -1;
102
+
103
+ let prevSourceNo = 0;
104
+ let prevSourceRowPos = 0;
105
+ let prevSourceColPos = 0;
106
+
107
+ let prevColPos = 0;
108
+ for (const meta of this.metas) {
109
+ while (meta.rowPos >= mappingRows.length) {
110
+ mappingRows.push([]);
111
+ }
112
+
113
+ if (lastRow != meta.rowPos || lastCol != meta.colPos) {
114
+ const currentRow = mappingRows[meta.rowPos];
115
+
116
+ if (lastRow != meta.rowPos) {
117
+ prevColPos = 0;
118
+ }
119
+ const mapping = mapper(meta.rowPos, meta.colPos, meta.pos, meta.item);
120
+ if (mapping) {
121
+ currentRow.push([
122
+ meta.colPos - prevColPos,
123
+ mapping.sourceNo - prevSourceNo,
124
+ mapping.sourceRowPos - prevSourceRowPos,
125
+ mapping.sourceColPos - prevSourceColPos,
126
+ ]);
127
+ prevColPos = meta.colPos;
128
+ prevSourceNo = mapping.sourceNo;
129
+ prevSourceRowPos = mapping.sourceRowPos;
130
+ prevSourceColPos = mapping.sourceColPos;
131
+ }
132
+ }
133
+
134
+ lastRow = meta.rowPos;
135
+ lastCol = meta.colPos;
136
+ }
137
+
138
+ const mappings = mappingRows
139
+ .map((row) =>
140
+ row.map(
141
+ (group) => encode(group),
142
+ )
143
+ .join(',')
144
+ )
145
+ .join(';');
146
+
147
+ return {
148
+ version: 3,
149
+ file: '',
150
+ sourceRoot: '',
151
+ sources: [],
152
+ sourcesContent: [],
153
+ names: [],
154
+ mappings,
155
+ };
156
+ }
157
+ }
158
+
159
+ const BASE64_CHARS =
160
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
161
+
162
+ function toVLQ(value: number): number[] {
163
+ const vlq: number[] = [];
164
+ const isNegative = value < 0;
165
+ value = Math.abs(value);
166
+
167
+ if (value === 0) {
168
+ vlq.push(0);
169
+ return vlq;
170
+ }
171
+
172
+ while (value > 0) {
173
+ let digit = value & 0x1F;
174
+ value >>= 5;
175
+ if (value > 0) {
176
+ digit |= 0x20;
177
+ }
178
+ vlq.push(digit);
179
+ if (vlq.length === 1) {
180
+ digit = isNegative ? (digit | 0x1) : (digit & ~0x1);
181
+ vlq[0] = digit;
182
+ }
183
+ }
184
+
185
+ return vlq;
186
+ }
187
+
188
+ function encodeVLQ(input: number | number[]): string {
189
+ const numbers = Array.isArray(input) ? input : [input];
190
+ let result = '';
191
+
192
+ for (const num of numbers) {
193
+ const vlq = toVLQ(num);
194
+ for (const digit of vlq) {
195
+ result += BASE64_CHARS[digit];
196
+ }
197
+ }
198
+
199
+ return result;
200
+ }
201
+
202
+ const char_to_integer: Record<string, number> = {};
203
+ const integer_to_char: Record<number, string> = {};
204
+
205
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
206
+ .split('')
207
+ .forEach(function (char, i) {
208
+ char_to_integer[char] = i;
209
+ integer_to_char[i] = char;
210
+ });
211
+
212
+ export function decode(string: string) {
213
+ let result: number[] = [];
214
+
215
+ let shift = 0;
216
+ let value = 0;
217
+
218
+ for (let i = 0; i < string.length; i += 1) {
219
+ let integer = char_to_integer[string[i]];
220
+
221
+ if (integer === undefined) {
222
+ throw new Error('Invalid character (' + string[i] + ')');
223
+ }
224
+
225
+ const has_continuation_bit = integer & 32;
226
+
227
+ integer &= 31;
228
+ value += integer << shift;
229
+
230
+ if (has_continuation_bit) {
231
+ shift += 5;
232
+ } else {
233
+ const should_negate = value & 1;
234
+ value >>>= 1;
235
+
236
+ if (should_negate) {
237
+ result.push(value === 0 ? -0x80000000 : -value);
238
+ } else {
239
+ result.push(value);
240
+ }
241
+
242
+ // reset
243
+ value = shift = 0;
244
+ }
245
+ }
246
+
247
+ return result;
248
+ }
249
+
250
+ export function encode(value: number | number[]) {
251
+ if (typeof value === 'number') {
252
+ return encode_integer(value);
253
+ }
254
+
255
+ let result = '';
256
+ for (let i = 0; i < value.length; i += 1) {
257
+ result += encode_integer(value[i]);
258
+ }
259
+
260
+ return result;
261
+ }
262
+
263
+ function encode_integer(num: number) {
264
+ let result = '';
265
+
266
+ if (num < 0) {
267
+ num = (-num << 1) | 1;
268
+ } else {
269
+ num <<= 1;
270
+ }
271
+
272
+ do {
273
+ let clamped = num & 31;
274
+ num >>>= 5;
275
+
276
+ if (num > 0) {
277
+ clamped |= 32;
278
+ }
279
+
280
+ result += integer_to_char[clamped];
281
+ } while (num > 0);
282
+
283
+ return result;
284
+ }
@@ -0,0 +1,66 @@
1
+ import { Fragment, Node as ProseMirrorNode, Schema } from 'prosemirror-model';
2
+
3
+ import type { Content, JSONContent } from '../types.js';
4
+
5
+ export type CreateNodeFromContentOptions = {
6
+ errorOnInvalidContent?: boolean;
7
+ };
8
+
9
+ export function createNodeFromObject(
10
+ content: JSONContent | ProseMirrorNode | Fragment,
11
+ schema: Schema,
12
+ options?: CreateNodeFromContentOptions,
13
+ ): ProseMirrorNode {
14
+ try {
15
+ const node = schema.nodeFromJSON(content);
16
+
17
+ if (options?.errorOnInvalidContent) {
18
+ node.check();
19
+ }
20
+
21
+ return node;
22
+ } catch (error) {
23
+ if (options?.errorOnInvalidContent) {
24
+ throw new Error('Invalid JSON content', {
25
+ cause: error as Error,
26
+ });
27
+ }
28
+
29
+ console.warn(
30
+ 'Invalid content.',
31
+ 'Passed value:',
32
+ content,
33
+ 'Error:',
34
+ error,
35
+ );
36
+
37
+ return schema.topNodeType.createAndFill(null, [])!;
38
+ }
39
+ }
40
+
41
+ export function createNodeFromArray(
42
+ content: JSONContent[],
43
+ schema: Schema,
44
+ ): Fragment {
45
+ return Fragment.fromArray(
46
+ content.map((item) => schema.nodeFromJSON(item)),
47
+ );
48
+ }
49
+
50
+ export function createNodeFromContent(
51
+ content: JSONContent | ProseMirrorNode | Fragment,
52
+ schema: Schema,
53
+ options?: CreateNodeFromContentOptions,
54
+ ): ProseMirrorNode | Fragment {
55
+ if (content instanceof ProseMirrorNode || content instanceof Fragment) {
56
+ return content;
57
+ }
58
+
59
+ const isJSONContent = typeof content === 'object' && content !== null;
60
+
61
+ if (isJSONContent) {
62
+ return createNodeFromObject(content, schema, options);
63
+ }
64
+
65
+ return schema.topNodeType.createAndFill(null, [])!;
66
+ }
@@ -0,0 +1,68 @@
1
+ import type {
2
+ MarkSpec,
3
+ Node as ProseMirrorNode,
4
+ NodeSpec,
5
+ } from 'prosemirror-model';
6
+
7
+ import type { Mark } from '../Mark.js';
8
+ import type { Node } from '../Node.js';
9
+
10
+ type MarkOfNode = Mark | Node;
11
+
12
+ export function getHtmlAttributes(
13
+ extension: MarkOfNode,
14
+ node: ProseMirrorNode,
15
+ ) {
16
+ const attrs: Record<string, any> = {};
17
+
18
+ if (extension.attributes) {
19
+ for (const [key, value] of Object.entries(extension.attributes)) {
20
+ if ('undefined' !== typeof node.attrs[key]) {
21
+ attrs[key] = node.attrs[key];
22
+ } else {
23
+ if (value.toDom) {
24
+ attrs[key] = value.toDom(node);
25
+ } else {
26
+ attrs[key] = value.default;
27
+ }
28
+ }
29
+ }
30
+ }
31
+
32
+ return attrs;
33
+ }
34
+
35
+ export function setHtmlAttributes(extension: MarkOfNode, element: HTMLElement) {
36
+ const attrs: Record<string, any> = {};
37
+
38
+ if (extension.attributes) {
39
+ for (const [key, value] of Object.entries(extension.attributes)) {
40
+ if (value.fromDom) {
41
+ attrs[key] = value.fromDom(element);
42
+ } else {
43
+ attrs[key] = value.default;
44
+ }
45
+ }
46
+ }
47
+
48
+ return attrs;
49
+ }
50
+
51
+ export function addAttributesToSchema(
52
+ spec: MarkSpec | NodeSpec,
53
+ extension: MarkOfNode,
54
+ ) {
55
+ const attrs = {};
56
+
57
+ if (extension.attributes) {
58
+ if (!spec.attrs) {
59
+ spec.attrs = {};
60
+ }
61
+ for (const [key, value] of Object.entries(extension.attributes)) {
62
+ spec.attrs[key] = value;
63
+ if (!value.toDom) {
64
+ value.toDom = (node) => node.attrs[key];
65
+ }
66
+ }
67
+ }
68
+ }
@@ -0,0 +1,18 @@
1
+ export function getShadowRoot(element?: Element): ShadowRoot | undefined {
2
+ let current = element;
3
+
4
+ while (current) {
5
+ if (current.toString() === '[object ShadowRoot]') {
6
+ return current as unknown as ShadowRoot;
7
+ }
8
+ if (current instanceof ShadowRoot) {
9
+ if (current.host) {
10
+ return current.host.shadowRoot || undefined;
11
+ }
12
+ }
13
+ current = current.parentElement ||
14
+ ((current instanceof ShadowRoot) ? current.host : undefined);
15
+ }
16
+
17
+ return undefined;
18
+ }
@@ -0,0 +1,5 @@
1
+ export * from './getHtmlAttributes.js';
2
+ export * from './createNodeFromContent.js';
3
+ export * from './SmartOutput.js';
4
+ export * from './getShadowRoot.js';
5
+ export * from './toRawTextResult.js';
@@ -0,0 +1,27 @@
1
+ import { RawTextMapEntry, RawTextResult } from '../types.js';
2
+
3
+ export function toRawTextResult(content: string, nodeIdx = 0): RawTextResult {
4
+ const lines = content.split('\n');
5
+
6
+ const rawTextMap: Array<RawTextMapEntry> = [];
7
+
8
+ let targetPos = 0;
9
+ let targetRow = 0;
10
+ for (const line of lines) {
11
+ rawTextMap.push({
12
+ nodeIdx,
13
+ targetRow,
14
+ targetCol: 0,
15
+ targetPos,
16
+ });
17
+
18
+ targetRow++;
19
+ targetPos += line.length + 1;
20
+ nodeIdx += line.length + 1;
21
+ }
22
+
23
+ return {
24
+ content,
25
+ rawTextMap,
26
+ };
27
+ }