@tiptap/core 2.5.8 → 2.6.0
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.
- package/dist/index.cjs +46 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +46 -21
- package/dist/index.js.map +1 -1
- package/dist/index.umd.js +46 -21
- package/dist/index.umd.js.map +1 -1
- package/dist/packages/core/src/Editor.d.ts +4 -0
- package/dist/packages/core/src/Extension.d.ts +5 -4
- package/dist/packages/core/src/Mark.d.ts +5 -4
- package/dist/packages/core/src/Node.d.ts +5 -4
- package/dist/packages/core/src/commands/setMeta.d.ts +2 -1
- package/dist/packages/core/src/commands/splitListItem.d.ts +2 -1
- package/dist/packages/core/src/helpers/isNodeEmpty.d.ts +10 -4
- package/dist/packages/core/src/pasteRules/nodePasteRule.d.ts +2 -1
- package/package.json +3 -3
- package/src/Editor.ts +6 -0
- package/src/Extension.ts +5 -4
- package/src/Mark.ts +5 -4
- package/src/Node.ts +5 -4
- package/src/commands/setMeta.ts +3 -1
- package/src/commands/splitListItem.ts +27 -17
- package/src/helpers/getMarksBetween.ts +1 -1
- package/src/helpers/isNodeEmpty.ts +33 -12
- package/src/pasteRules/nodePasteRule.ts +14 -5
- package/src/utilities/findDuplicates.ts +1 -1
|
@@ -16,6 +16,10 @@ export declare class Editor extends EventEmitter<EditorEvents> {
|
|
|
16
16
|
schema: Schema;
|
|
17
17
|
view: EditorView;
|
|
18
18
|
isFocused: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* The editor is considered initialized after the `create` event has been emitted.
|
|
21
|
+
*/
|
|
22
|
+
isInitialized: boolean;
|
|
19
23
|
extensionStorage: Record<string, any>;
|
|
20
24
|
options: EditorOptions;
|
|
21
25
|
constructor(options?: Partial<EditorOptions>);
|
|
@@ -17,10 +17,10 @@ declare module '@tiptap/core' {
|
|
|
17
17
|
*/
|
|
18
18
|
name: string;
|
|
19
19
|
/**
|
|
20
|
-
* The priority of your extension. The higher, the
|
|
20
|
+
* The priority of your extension. The higher, the earlier it will be called
|
|
21
21
|
* and will take precedence over other extensions with a lower priority.
|
|
22
|
-
* @default
|
|
23
|
-
* @example
|
|
22
|
+
* @default 100
|
|
23
|
+
* @example 101
|
|
24
24
|
*/
|
|
25
25
|
priority?: number;
|
|
26
26
|
/**
|
|
@@ -286,6 +286,7 @@ declare module '@tiptap/core' {
|
|
|
286
286
|
editor: Editor;
|
|
287
287
|
parent: ParentConfig<ExtensionConfig<Options, Storage>>['onTransaction'];
|
|
288
288
|
}, props: {
|
|
289
|
+
editor: Editor;
|
|
289
290
|
transaction: Transaction;
|
|
290
291
|
}) => void) | null;
|
|
291
292
|
/**
|
|
@@ -338,6 +339,6 @@ export declare class Extension<Options = any, Storage = any> {
|
|
|
338
339
|
config: ExtensionConfig;
|
|
339
340
|
constructor(config?: Partial<ExtensionConfig<Options, Storage>>);
|
|
340
341
|
static create<O = any, S = any>(config?: Partial<ExtensionConfig<O, S>>): Extension<O, S>;
|
|
341
|
-
configure(options?: Partial<Options>): Extension<
|
|
342
|
+
configure(options?: Partial<Options>): Extension<Options, Storage>;
|
|
342
343
|
extend<ExtendedOptions = Options, ExtendedStorage = Storage>(extendedConfig?: Partial<ExtensionConfig<ExtendedOptions, ExtendedStorage>>): Extension<ExtendedOptions, ExtendedStorage>;
|
|
343
344
|
}
|
|
@@ -17,10 +17,10 @@ declare module '@tiptap/core' {
|
|
|
17
17
|
*/
|
|
18
18
|
name: string;
|
|
19
19
|
/**
|
|
20
|
-
* The priority of your extension. The higher, the
|
|
20
|
+
* The priority of your extension. The higher, the earlier it will be called
|
|
21
21
|
* and will take precedence over other extensions with a lower priority.
|
|
22
|
-
* @default
|
|
23
|
-
* @example
|
|
22
|
+
* @default 100
|
|
23
|
+
* @example 101
|
|
24
24
|
*/
|
|
25
25
|
priority?: number;
|
|
26
26
|
/**
|
|
@@ -296,6 +296,7 @@ declare module '@tiptap/core' {
|
|
|
296
296
|
type: MarkType;
|
|
297
297
|
parent: ParentConfig<MarkConfig<Options, Storage>>['onTransaction'];
|
|
298
298
|
}, props: {
|
|
299
|
+
editor: Editor;
|
|
299
300
|
transaction: Transaction;
|
|
300
301
|
}) => void) | null;
|
|
301
302
|
/**
|
|
@@ -442,7 +443,7 @@ export declare class Mark<Options = any, Storage = any> {
|
|
|
442
443
|
config: MarkConfig;
|
|
443
444
|
constructor(config?: Partial<MarkConfig<Options, Storage>>);
|
|
444
445
|
static create<O = any, S = any>(config?: Partial<MarkConfig<O, S>>): Mark<O, S>;
|
|
445
|
-
configure(options?: Partial<Options>): Mark<
|
|
446
|
+
configure(options?: Partial<Options>): Mark<Options, Storage>;
|
|
446
447
|
extend<ExtendedOptions = Options, ExtendedStorage = Storage>(extendedConfig?: Partial<MarkConfig<ExtendedOptions, ExtendedStorage>>): Mark<ExtendedOptions, ExtendedStorage>;
|
|
447
448
|
static handleExit({ editor, mark }: {
|
|
448
449
|
editor: Editor;
|
|
@@ -17,10 +17,10 @@ declare module '@tiptap/core' {
|
|
|
17
17
|
*/
|
|
18
18
|
name: string;
|
|
19
19
|
/**
|
|
20
|
-
* The priority of your extension. The higher, the
|
|
20
|
+
* The priority of your extension. The higher, the earlier it will be called
|
|
21
21
|
* and will take precedence over other extensions with a lower priority.
|
|
22
|
-
* @default
|
|
23
|
-
* @example
|
|
22
|
+
* @default 100
|
|
23
|
+
* @example 101
|
|
24
24
|
*/
|
|
25
25
|
priority?: number;
|
|
26
26
|
/**
|
|
@@ -297,6 +297,7 @@ declare module '@tiptap/core' {
|
|
|
297
297
|
type: NodeType;
|
|
298
298
|
parent: ParentConfig<NodeConfig<Options, Storage>>['onTransaction'];
|
|
299
299
|
}, props: {
|
|
300
|
+
editor: Editor;
|
|
300
301
|
transaction: Transaction;
|
|
301
302
|
}) => void) | null;
|
|
302
303
|
/**
|
|
@@ -606,6 +607,6 @@ export declare class Node<Options = any, Storage = any> {
|
|
|
606
607
|
config: NodeConfig;
|
|
607
608
|
constructor(config?: Partial<NodeConfig<Options, Storage>>);
|
|
608
609
|
static create<O = any, S = any>(config?: Partial<NodeConfig<O, S>>): Node<O, S>;
|
|
609
|
-
configure(options?: Partial<Options>): Node<
|
|
610
|
+
configure(options?: Partial<Options>): Node<Options, Storage>;
|
|
610
611
|
extend<ExtendedOptions = Options, ExtendedStorage = Storage>(extendedConfig?: Partial<NodeConfig<ExtendedOptions, ExtendedStorage>>): Node<ExtendedOptions, ExtendedStorage>;
|
|
611
612
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Plugin, PluginKey } from '@tiptap/pm/state';
|
|
1
2
|
import { RawCommands } from '../types.js';
|
|
2
3
|
declare module '@tiptap/core' {
|
|
3
4
|
interface Commands<ReturnType> {
|
|
@@ -8,7 +9,7 @@ declare module '@tiptap/core' {
|
|
|
8
9
|
* @param value The value to store.
|
|
9
10
|
* @example editor.commands.setMeta('foo', 'bar')
|
|
10
11
|
*/
|
|
11
|
-
setMeta: (key: string, value: any) => ReturnType;
|
|
12
|
+
setMeta: (key: string | Plugin | PluginKey, value: any) => ReturnType;
|
|
12
13
|
};
|
|
13
14
|
}
|
|
14
15
|
}
|
|
@@ -6,9 +6,10 @@ declare module '@tiptap/core' {
|
|
|
6
6
|
/**
|
|
7
7
|
* Splits one list item into two list items.
|
|
8
8
|
* @param typeOrName The type or name of the node.
|
|
9
|
+
* @param overrideAttrs The attributes to ensure on the new node.
|
|
9
10
|
* @example editor.commands.splitListItem('listItem')
|
|
10
11
|
*/
|
|
11
|
-
splitListItem: (typeOrName: string | NodeType) => ReturnType;
|
|
12
|
+
splitListItem: (typeOrName: string | NodeType, overrideAttrs?: Record<string, any>) => ReturnType;
|
|
12
13
|
};
|
|
13
14
|
}
|
|
14
15
|
}
|
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import { Node as ProseMirrorNode } from '@tiptap/pm/model';
|
|
2
2
|
/**
|
|
3
|
-
* Returns true if the given node is empty.
|
|
4
|
-
* When `checkChildren` is true (default), it will also check if all children are empty.
|
|
3
|
+
* Returns true if the given prosemirror node is empty.
|
|
5
4
|
*/
|
|
6
|
-
export declare function isNodeEmpty(node: ProseMirrorNode, { checkChildren }?: {
|
|
7
|
-
|
|
5
|
+
export declare function isNodeEmpty(node: ProseMirrorNode, { checkChildren, ignoreWhitespace, }?: {
|
|
6
|
+
/**
|
|
7
|
+
* When true (default), it will also check if all children are empty.
|
|
8
|
+
*/
|
|
9
|
+
checkChildren?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* When true, it will ignore whitespace when checking for emptiness.
|
|
12
|
+
*/
|
|
13
|
+
ignoreWhitespace?: boolean;
|
|
8
14
|
}): boolean;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { NodeType } from '@tiptap/pm/model';
|
|
2
2
|
import { PasteRule, PasteRuleFinder } from '../PasteRule.js';
|
|
3
|
-
import { ExtendedRegExpMatchArray } from '../types.js';
|
|
3
|
+
import { ExtendedRegExpMatchArray, JSONContent } from '../types.js';
|
|
4
4
|
/**
|
|
5
5
|
* Build an paste rule that adds a node when the
|
|
6
6
|
* matched text is pasted into it.
|
|
@@ -10,4 +10,5 @@ export declare function nodePasteRule(config: {
|
|
|
10
10
|
find: PasteRuleFinder;
|
|
11
11
|
type: NodeType;
|
|
12
12
|
getAttributes?: Record<string, any> | ((match: ExtendedRegExpMatchArray, event: ClipboardEvent) => Record<string, any>) | false | null;
|
|
13
|
+
getContent?: JSONContent[] | ((attrs: Record<string, any>) => JSONContent[]) | false | null;
|
|
13
14
|
}): PasteRule;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tiptap/core",
|
|
3
3
|
"description": "headless rich text editor",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.6.0",
|
|
5
5
|
"homepage": "https://tiptap.dev",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"tiptap",
|
|
@@ -32,10 +32,10 @@
|
|
|
32
32
|
"dist"
|
|
33
33
|
],
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@tiptap/pm": "^2.
|
|
35
|
+
"@tiptap/pm": "^2.6.0"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"@tiptap/pm": "^2.
|
|
38
|
+
"@tiptap/pm": "^2.6.0"
|
|
39
39
|
},
|
|
40
40
|
"repository": {
|
|
41
41
|
"type": "git",
|
package/src/Editor.ts
CHANGED
|
@@ -57,6 +57,11 @@ export class Editor extends EventEmitter<EditorEvents> {
|
|
|
57
57
|
|
|
58
58
|
public isFocused = false
|
|
59
59
|
|
|
60
|
+
/**
|
|
61
|
+
* The editor is considered initialized after the `create` event has been emitted.
|
|
62
|
+
*/
|
|
63
|
+
public isInitialized = false
|
|
64
|
+
|
|
60
65
|
public extensionStorage: Record<string, any> = {}
|
|
61
66
|
|
|
62
67
|
public options: EditorOptions = {
|
|
@@ -111,6 +116,7 @@ export class Editor extends EventEmitter<EditorEvents> {
|
|
|
111
116
|
|
|
112
117
|
this.commands.focus(this.options.autofocus)
|
|
113
118
|
this.emit('create', { editor: this })
|
|
119
|
+
this.isInitialized = true
|
|
114
120
|
}, 0)
|
|
115
121
|
}
|
|
116
122
|
|
package/src/Extension.ts
CHANGED
|
@@ -32,10 +32,10 @@ declare module '@tiptap/core' {
|
|
|
32
32
|
name: string
|
|
33
33
|
|
|
34
34
|
/**
|
|
35
|
-
* The priority of your extension. The higher, the
|
|
35
|
+
* The priority of your extension. The higher, the earlier it will be called
|
|
36
36
|
* and will take precedence over other extensions with a lower priority.
|
|
37
|
-
* @default
|
|
38
|
-
* @example
|
|
37
|
+
* @default 100
|
|
38
|
+
* @example 101
|
|
39
39
|
*/
|
|
40
40
|
priority?: number
|
|
41
41
|
|
|
@@ -339,6 +339,7 @@ declare module '@tiptap/core' {
|
|
|
339
339
|
parent: ParentConfig<ExtensionConfig<Options, Storage>>['onTransaction']
|
|
340
340
|
},
|
|
341
341
|
props: {
|
|
342
|
+
editor: Editor
|
|
342
343
|
transaction: Transaction
|
|
343
344
|
},
|
|
344
345
|
) => void)
|
|
@@ -457,7 +458,7 @@ export class Extension<Options = any, Storage = any> {
|
|
|
457
458
|
configure(options: Partial<Options> = {}) {
|
|
458
459
|
// return a new instance so we can use the same extension
|
|
459
460
|
// with different calls of `configure`
|
|
460
|
-
const extension = this.extend({
|
|
461
|
+
const extension = this.extend<Options, Storage>({
|
|
461
462
|
...this.config,
|
|
462
463
|
addOptions: () => {
|
|
463
464
|
return mergeDeep(this.options as Record<string, any>, options) as Options
|
package/src/Mark.ts
CHANGED
|
@@ -35,10 +35,10 @@ declare module '@tiptap/core' {
|
|
|
35
35
|
name: string
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
|
-
* The priority of your extension. The higher, the
|
|
38
|
+
* The priority of your extension. The higher, the earlier it will be called
|
|
39
39
|
* and will take precedence over other extensions with a lower priority.
|
|
40
|
-
* @default
|
|
41
|
-
* @example
|
|
40
|
+
* @default 100
|
|
41
|
+
* @example 101
|
|
42
42
|
*/
|
|
43
43
|
priority?: number
|
|
44
44
|
|
|
@@ -352,6 +352,7 @@ declare module '@tiptap/core' {
|
|
|
352
352
|
parent: ParentConfig<MarkConfig<Options, Storage>>['onTransaction']
|
|
353
353
|
},
|
|
354
354
|
props: {
|
|
355
|
+
editor: Editor
|
|
355
356
|
transaction: Transaction
|
|
356
357
|
},
|
|
357
358
|
) => void)
|
|
@@ -589,7 +590,7 @@ export class Mark<Options = any, Storage = any> {
|
|
|
589
590
|
configure(options: Partial<Options> = {}) {
|
|
590
591
|
// return a new instance so we can use the same extension
|
|
591
592
|
// with different calls of `configure`
|
|
592
|
-
const extension = this.extend({
|
|
593
|
+
const extension = this.extend<Options, Storage>({
|
|
593
594
|
...this.config,
|
|
594
595
|
addOptions: () => {
|
|
595
596
|
return mergeDeep(this.options as Record<string, any>, options) as Options
|
package/src/Node.ts
CHANGED
|
@@ -36,10 +36,10 @@ declare module '@tiptap/core' {
|
|
|
36
36
|
name: string
|
|
37
37
|
|
|
38
38
|
/**
|
|
39
|
-
* The priority of your extension. The higher, the
|
|
39
|
+
* The priority of your extension. The higher, the earlier it will be called
|
|
40
40
|
* and will take precedence over other extensions with a lower priority.
|
|
41
|
-
* @default
|
|
42
|
-
* @example
|
|
41
|
+
* @default 100
|
|
42
|
+
* @example 101
|
|
43
43
|
*/
|
|
44
44
|
priority?: number
|
|
45
45
|
|
|
@@ -354,6 +354,7 @@ declare module '@tiptap/core' {
|
|
|
354
354
|
parent: ParentConfig<NodeConfig<Options, Storage>>['onTransaction']
|
|
355
355
|
},
|
|
356
356
|
props: {
|
|
357
|
+
editor: Editor
|
|
357
358
|
transaction: Transaction
|
|
358
359
|
},
|
|
359
360
|
) => void)
|
|
@@ -780,7 +781,7 @@ export class Node<Options = any, Storage = any> {
|
|
|
780
781
|
configure(options: Partial<Options> = {}) {
|
|
781
782
|
// return a new instance so we can use the same extension
|
|
782
783
|
// with different calls of `configure`
|
|
783
|
-
const extension = this.extend({
|
|
784
|
+
const extension = this.extend<Options, Storage>({
|
|
784
785
|
...this.config,
|
|
785
786
|
addOptions: () => {
|
|
786
787
|
return mergeDeep(this.options as Record<string, any>, options) as Options
|
package/src/commands/setMeta.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { Plugin, PluginKey } from '@tiptap/pm/state'
|
|
2
|
+
|
|
1
3
|
import { RawCommands } from '../types.js'
|
|
2
4
|
|
|
3
5
|
declare module '@tiptap/core' {
|
|
@@ -9,7 +11,7 @@ declare module '@tiptap/core' {
|
|
|
9
11
|
* @param value The value to store.
|
|
10
12
|
* @example editor.commands.setMeta('foo', 'bar')
|
|
11
13
|
*/
|
|
12
|
-
setMeta: (key: string, value: any) => ReturnType,
|
|
14
|
+
setMeta: (key: string | Plugin | PluginKey, value: any) => ReturnType,
|
|
13
15
|
}
|
|
14
16
|
}
|
|
15
17
|
}
|
|
@@ -14,14 +14,15 @@ declare module '@tiptap/core' {
|
|
|
14
14
|
/**
|
|
15
15
|
* Splits one list item into two list items.
|
|
16
16
|
* @param typeOrName The type or name of the node.
|
|
17
|
+
* @param overrideAttrs The attributes to ensure on the new node.
|
|
17
18
|
* @example editor.commands.splitListItem('listItem')
|
|
18
19
|
*/
|
|
19
|
-
splitListItem: (typeOrName: string | NodeType) => ReturnType
|
|
20
|
+
splitListItem: (typeOrName: string | NodeType, overrideAttrs?: Record<string, any>) => ReturnType
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
export const splitListItem: RawCommands['splitListItem'] = typeOrName => ({
|
|
25
|
+
export const splitListItem: RawCommands['splitListItem'] = (typeOrName, overrideAttrs = {}) => ({
|
|
25
26
|
tr, state, dispatch, editor,
|
|
26
27
|
}) => {
|
|
27
28
|
const type = getNodeType(typeOrName, state.schema)
|
|
@@ -70,11 +71,14 @@ export const splitListItem: RawCommands['splitListItem'] = typeOrName => ({
|
|
|
70
71
|
const depthAfter = $from.indexAfter(-1) < $from.node(-2).childCount ? 1 : $from.indexAfter(-2) < $from.node(-3).childCount ? 2 : 3
|
|
71
72
|
|
|
72
73
|
// Add a second list item with an empty default start node
|
|
73
|
-
const newNextTypeAttributes =
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
74
|
+
const newNextTypeAttributes = {
|
|
75
|
+
...getSplittedAttributes(
|
|
76
|
+
extensionAttributes,
|
|
77
|
+
$from.node().type.name,
|
|
78
|
+
$from.node().attrs,
|
|
79
|
+
),
|
|
80
|
+
...overrideAttrs,
|
|
81
|
+
}
|
|
78
82
|
const nextType = type.contentMatch.defaultType?.createAndFill(newNextTypeAttributes) || undefined
|
|
79
83
|
|
|
80
84
|
wrap = wrap.append(Fragment.from(type.createAndFill(null, nextType) || undefined))
|
|
@@ -107,16 +111,22 @@ export const splitListItem: RawCommands['splitListItem'] = typeOrName => ({
|
|
|
107
111
|
|
|
108
112
|
const nextType = $to.pos === $from.end() ? grandParent.contentMatchAt(0).defaultType : null
|
|
109
113
|
|
|
110
|
-
const newTypeAttributes =
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
114
|
+
const newTypeAttributes = {
|
|
115
|
+
...getSplittedAttributes(
|
|
116
|
+
extensionAttributes,
|
|
117
|
+
grandParent.type.name,
|
|
118
|
+
grandParent.attrs,
|
|
119
|
+
),
|
|
120
|
+
...overrideAttrs,
|
|
121
|
+
}
|
|
122
|
+
const newNextTypeAttributes = {
|
|
123
|
+
...getSplittedAttributes(
|
|
124
|
+
extensionAttributes,
|
|
125
|
+
$from.node().type.name,
|
|
126
|
+
$from.node().attrs,
|
|
127
|
+
),
|
|
128
|
+
...overrideAttrs,
|
|
129
|
+
}
|
|
120
130
|
|
|
121
131
|
tr.delete($from.pos, $to.pos)
|
|
122
132
|
|
|
@@ -12,7 +12,7 @@ export function getMarksBetween(from: number, to: number, doc: ProseMirrorNode):
|
|
|
12
12
|
.resolve(from)
|
|
13
13
|
.marks()
|
|
14
14
|
.forEach(mark => {
|
|
15
|
-
const $pos = doc.resolve(from
|
|
15
|
+
const $pos = doc.resolve(from)
|
|
16
16
|
const range = getMarkRange($pos, mark.type)
|
|
17
17
|
|
|
18
18
|
if (!range) {
|
|
@@ -1,40 +1,61 @@
|
|
|
1
1
|
import { Node as ProseMirrorNode } from '@tiptap/pm/model'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Returns true if the given node is empty.
|
|
5
|
-
* When `checkChildren` is true (default), it will also check if all children are empty.
|
|
4
|
+
* Returns true if the given prosemirror node is empty.
|
|
6
5
|
*/
|
|
7
6
|
export function isNodeEmpty(
|
|
8
7
|
node: ProseMirrorNode,
|
|
9
|
-
{
|
|
8
|
+
{
|
|
9
|
+
checkChildren = true,
|
|
10
|
+
ignoreWhitespace = false,
|
|
11
|
+
}: {
|
|
12
|
+
/**
|
|
13
|
+
* When true (default), it will also check if all children are empty.
|
|
14
|
+
*/
|
|
15
|
+
checkChildren?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* When true, it will ignore whitespace when checking for emptiness.
|
|
18
|
+
*/
|
|
19
|
+
ignoreWhitespace?: boolean;
|
|
20
|
+
} = {},
|
|
10
21
|
): boolean {
|
|
22
|
+
if (ignoreWhitespace) {
|
|
23
|
+
if (node.type.name === 'hardBreak') {
|
|
24
|
+
// Hard breaks are considered empty
|
|
25
|
+
return true
|
|
26
|
+
}
|
|
27
|
+
if (node.isText) {
|
|
28
|
+
return /^\s*$/m.test(node.text ?? '')
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
11
32
|
if (node.isText) {
|
|
12
33
|
return !node.text
|
|
13
34
|
}
|
|
14
35
|
|
|
15
|
-
if (node.
|
|
16
|
-
return
|
|
36
|
+
if (node.isAtom || node.isLeaf) {
|
|
37
|
+
return false
|
|
17
38
|
}
|
|
18
39
|
|
|
19
|
-
if (node.
|
|
20
|
-
return
|
|
40
|
+
if (node.content.childCount === 0) {
|
|
41
|
+
return true
|
|
21
42
|
}
|
|
22
43
|
|
|
23
44
|
if (checkChildren) {
|
|
24
|
-
let
|
|
45
|
+
let isContentEmpty = true
|
|
25
46
|
|
|
26
47
|
node.content.forEach(childNode => {
|
|
27
|
-
if (
|
|
48
|
+
if (isContentEmpty === false) {
|
|
28
49
|
// Exit early for perf
|
|
29
50
|
return
|
|
30
51
|
}
|
|
31
52
|
|
|
32
|
-
if (!isNodeEmpty(childNode)) {
|
|
33
|
-
|
|
53
|
+
if (!isNodeEmpty(childNode, { ignoreWhitespace, checkChildren })) {
|
|
54
|
+
isContentEmpty = false
|
|
34
55
|
}
|
|
35
56
|
})
|
|
36
57
|
|
|
37
|
-
return
|
|
58
|
+
return isContentEmpty
|
|
38
59
|
}
|
|
39
60
|
|
|
40
61
|
return false
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { NodeType } from '@tiptap/pm/model'
|
|
2
2
|
|
|
3
3
|
import { PasteRule, PasteRuleFinder } from '../PasteRule.js'
|
|
4
|
-
import { ExtendedRegExpMatchArray } from '../types.js'
|
|
4
|
+
import { ExtendedRegExpMatchArray, JSONContent } from '../types.js'
|
|
5
5
|
import { callOrReturn } from '../utilities/index.js'
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -17,6 +17,11 @@ export function nodePasteRule(config: {
|
|
|
17
17
|
| ((match: ExtendedRegExpMatchArray, event: ClipboardEvent) => Record<string, any>)
|
|
18
18
|
| false
|
|
19
19
|
| null
|
|
20
|
+
getContent?:
|
|
21
|
+
| JSONContent[]
|
|
22
|
+
| ((attrs: Record<string, any>) => JSONContent[])
|
|
23
|
+
| false
|
|
24
|
+
| null
|
|
20
25
|
}) {
|
|
21
26
|
return new PasteRule({
|
|
22
27
|
find: config.find,
|
|
@@ -24,16 +29,20 @@ export function nodePasteRule(config: {
|
|
|
24
29
|
match, chain, range, pasteEvent,
|
|
25
30
|
}) {
|
|
26
31
|
const attributes = callOrReturn(config.getAttributes, undefined, match, pasteEvent)
|
|
32
|
+
const content = callOrReturn(config.getContent, undefined, attributes)
|
|
27
33
|
|
|
28
34
|
if (attributes === false || attributes === null) {
|
|
29
35
|
return null
|
|
30
36
|
}
|
|
31
37
|
|
|
38
|
+
const node = { type: config.type.name, attrs: attributes } as JSONContent
|
|
39
|
+
|
|
40
|
+
if (content) {
|
|
41
|
+
node.content = content
|
|
42
|
+
}
|
|
43
|
+
|
|
32
44
|
if (match.input) {
|
|
33
|
-
chain().deleteRange(range).insertContentAt(range.from,
|
|
34
|
-
type: config.type.name,
|
|
35
|
-
attrs: attributes,
|
|
36
|
-
})
|
|
45
|
+
chain().deleteRange(range).insertContentAt(range.from, node)
|
|
37
46
|
}
|
|
38
47
|
},
|
|
39
48
|
})
|