/
Block.ts
318 lines (293 loc) · 9.52 KB
/
Block.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import { mergeAttributes, Node } from "@tiptap/core";
import { Selection } from "prosemirror-state";
import styles from "./Block.module.css";
import { PreviousBlockTypePlugin } from "../PreviousBlockTypePlugin";
import { textblockTypeInputRuleSameNodeType } from "../rule";
export interface IBlock {
HTMLAttributes: Record<string, any>;
}
export type Level = 1 | 2 | 3;
declare module "@tiptap/core" {
interface Commands<ReturnType> {
blockHeading: {
/**
* Set a heading node
*/
setBlockHeading: (attributes: { level: Level }) => ReturnType;
/**
* Unset a heading node
*/
unsetBlockHeading: () => ReturnType;
unsetList: () => ReturnType;
};
}
}
/**
* The main "Block node" documents consist of
*/
export const Block = Node.create<IBlock>({
name: "tcblock",
group: "block",
addOptions() {
return {
HTMLAttributes: {},
};
},
// A block always contains content, and optionally a blockGroup which contains nested blocks
content: "tccontent blockGroup?",
defining: true,
addAttributes() {
return {
listType: {
default: undefined,
renderHTML: (attributes) => {
return {
"data-listType": attributes.listType,
};
},
parseHTML: (element) => element.getAttribute("data-listType"),
},
blockColor: {
default: undefined,
renderHTML: (attributes) => {
return {
"data-blockColor": attributes.blockColor,
};
},
parseHTML: (element) => element.getAttribute("data-blockColor"),
},
blockStyle: {
default: undefined,
renderHTML: (attributes) => {
return {
"data-blockStyle": attributes.blockStyle,
};
},
parseHTML: (element) => element.getAttribute("data-blockStyle"),
},
headingType: {
default: undefined,
keepOnSplit: false,
renderHTML: (attributes) => {
return {
"data-headingType": attributes.headingType,
};
},
parseHTML: (element) => element.getAttribute("data-headingType"),
},
};
},
// TODO: should we parse <li>, <ol>, <h1>, etc?
parseHTML() {
return [
{
tag: "div",
},
];
},
renderHTML({ HTMLAttributes }) {
return [
"div",
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {
class: styles.blockOuter,
}),
[
"div",
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {
class: styles.block,
}),
0,
],
];
},
addInputRules() {
return [
...[1, 2, 3].map((level) => {
// Create a heading when starting with "#", "##", or "###""
return textblockTypeInputRuleSameNodeType({
find: new RegExp(`^(#{1,${level}})\\s$`),
type: this.type,
getAttributes: {
headingType: level,
},
});
}),
// Create a list when starting with "-"
textblockTypeInputRuleSameNodeType({
find: /^\s*([-+*])\s$/,
type: this.type,
getAttributes: {
listType: "li",
},
}),
];
},
addCommands() {
return {
setBlockHeading:
(attributes) =>
({ commands }) => {
return commands.updateAttributes(this.name, {
headingType: attributes.level,
});
},
unsetBlockHeading:
() =>
({ commands }) => {
debugger;
return commands.updateAttributes(this.name, {
headingType: undefined,
});
},
unsetList:
() =>
({ tr, dispatch }) => {
const node = tr.selection.$anchor.node(-1);
const nodePos = tr.selection.$anchor.posAtIndex(0, -1) - 1;
// const node2 = tr.doc.nodeAt(nodePos);
if (node.type.name === "tcblock" && node.attrs["listType"]) {
if (dispatch) {
tr.setNodeMarkup(nodePos, undefined, {
...node.attrs,
listType: undefined,
});
}
return true;
}
return false;
},
};
},
addProseMirrorPlugins() {
return [PreviousBlockTypePlugin()];
},
addKeyboardShortcuts() {
// handleBackspace is partially adapted from https://github.com/ueberdosis/tiptap/blob/ed56337470efb4fd277128ab7ef792b37cfae992/packages/core/src/extensions/keymap.ts
const handleBackspace = () =>
this.editor.commands.first(({ commands }) => [
// Maybe the user wants to undo an auto formatting input rule (e.g.: - or #, and then hit backspace) (source: tiptap)
() => commands.undoInputRule(),
// maybe convert first text block node to default node (source: tiptap)
() =>
commands.command(({ tr }) => {
const { selection, doc } = tr;
const { empty, $anchor } = selection;
const { pos, parent } = $anchor;
const isAtStart = Selection.atStart(doc).from === pos;
if (
!empty ||
!isAtStart ||
!parent.type.isTextblock ||
parent.textContent.length
) {
return false;
}
return commands.clearNodes();
}),
() => commands.deleteSelection(), // (source: tiptap)
() =>
commands.command(({ tr }) => {
const isAtStartOfNode = tr.selection.$anchor.parentOffset === 0;
const node = tr.selection.$anchor.node(-1);
if (isAtStartOfNode && node.type.name === "tcblock") {
// we're at the start of the block, so we're trying to "backspace" the bullet or indentation
return commands.first([
() => commands.unsetList(), // first try to remove the "list" property
() => commands.liftListItem("tcblock"), // then try to remove a level of indentation
]);
}
return false;
}),
({ chain }) =>
// we are at the start of a block at the root level. The user hits backspace to "merge it" to the end of the block above
//
// BlockA
// BlockB
// Becomes:
// BlockABlockB
chain()
.command(({ tr, state, dispatch }) => {
const isAtStartOfNode = tr.selection.$anchor.parentOffset === 0;
const anchor = tr.selection.$anchor;
const node = anchor.node(-1);
if (isAtStartOfNode && node.type.name === "tcblock") {
if (node.childCount === 2) {
// BlockB has children. We want to go from this:
//
// BlockA
// BlockB
// BlockC
// BlockD
//
// to:
//
// BlockABlockB
// BlockC
// BlockD
// This parts moves the children of BlockB to the top level
const startSecondChild = anchor.posAtIndex(1, -1) + 1; // start of blockgroup
const endSecondChild = anchor.posAtIndex(2, -1) - 1;
const range = state.doc
.resolve(startSecondChild)
.blockRange(state.doc.resolve(endSecondChild));
if (dispatch) {
tr.lift(range!, anchor.depth - 2);
}
}
return true;
}
return false;
})
// use joinBackward to merge BlockB to BlockA (i.e.: turn it into BlockABlockB)
// The standard JoinBackward would break here, and would turn it into:
// BlockA
// BlockB
//
// joinBackward has been patched with patch-package (see /patches) to prevent this behaviour
.joinBackward()
.run(),
() => commands.selectNodeBackward(), // (source: tiptap)
]);
const handleEnter = () =>
this.editor.commands.first(({ commands }) => [
// Try to split the current block into 2 items:
() => commands.splitListItem("tcblock"),
// Otherwise, maybe we are in an empty list item. "Enter" should remove the list bullet
({ tr, dispatch }) => {
const $from = tr.selection.$from;
if ($from.depth !== 3) {
// only needed at root level, at deeper levels it should be handled already by splitListItem
return false;
}
const node = tr.selection.$anchor.node(-1);
const nodePos = tr.selection.$anchor.posAtIndex(0, -1) - 1;
if (node.type.name === "tcblock" && node.attrs["listType"]) {
if (dispatch) {
tr.setNodeMarkup(nodePos, undefined, {
...node.attrs,
listType: undefined,
});
}
return true;
}
return false;
},
// Otherwise, we might be on an empty line and hit "Enter" to create a new line:
({ tr, dispatch }) => {
const $from = tr.selection.$from;
if (dispatch) {
tr.split($from.pos, 2).scrollIntoView();
}
return true;
},
]);
return {
Backspace: handleBackspace,
Enter: handleEnter,
Tab: () => this.editor.commands.sinkListItem("tcblock"),
"Shift-Tab": () => {
return this.editor.commands.liftListItem("tcblock");
},
};
},
});