-
Notifications
You must be signed in to change notification settings - Fork 965
Expand file tree
/
Copy pathwithCodeBlock.ts
More file actions
139 lines (112 loc) · 3.7 KB
/
withCodeBlock.ts
File metadata and controls
139 lines (112 loc) · 3.7 KB
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
import type { OverrideEditor, TCodeBlockElement, TElement } from 'platejs';
import type { CodeBlockConfig } from './BaseCodeBlockPlugin';
import { getCodeLineEntry, getIndentDepth } from './queries';
import { resetCodeBlockDecorations } from './setCodeBlockToDecorations';
import { indentCodeLine, outdentCodeLine, unwrapCodeBlock } from './transforms';
import { withInsertDataCodeBlock } from './withInsertDataCodeBlock';
import { withInsertFragmentCodeBlock } from './withInsertFragmentCodeBlock';
import { withNormalizeCodeBlock } from './withNormalizeCodeBlock';
export const withCodeBlock: OverrideEditor<CodeBlockConfig> = (ctx) => {
const {
editor,
getOptions,
tf: { apply, insertBreak, resetBlock, selectAll, tab },
type,
} = ctx;
return {
transforms: {
apply(operation) {
if (getOptions().lowlight && operation.type === 'set_node') {
const entry = editor.api.node(operation.path);
if (entry?.[0].type === type && operation.newProperties?.lang) {
// Clear decorations for all code lines in this block
resetCodeBlockDecorations(entry[0] as TCodeBlockElement);
}
}
apply(operation);
},
insertBreak() {
const apply = () => {
if (!editor.selection) return;
const res = getCodeLineEntry(editor, {});
if (!res) return;
const { codeBlock, codeLine } = res;
const indentDepth = getIndentDepth(editor, {
codeBlock,
codeLine,
});
insertBreak();
indentCodeLine(editor, {
codeBlock,
codeLine,
indentDepth,
});
// Reset decoration cache so all lines get re-highlighted
resetCodeBlockDecorations(codeBlock[0] as TCodeBlockElement);
return true;
};
if (apply()) return;
insertBreak();
},
resetBlock(options) {
if (
editor.api.block({
at: options?.at,
match: { type },
})
) {
unwrapCodeBlock(editor);
return;
}
return resetBlock(options);
},
selectAll: () => {
const apply = () => {
const codeBlock = editor.api.above({
match: { type },
});
if (!codeBlock) return;
if (
editor.api.isAt({ end: true }) &&
editor.api.isAt({ start: true })
) {
return;
}
// Select the whole code block
editor.tf.select(codeBlock[1]);
return true;
};
if (apply()) return true;
return selectAll();
},
tab: (options) => {
const apply = () => {
const _codeLines = editor.api.nodes<TElement>({
match: { type },
});
const codeLines = Array.from(_codeLines);
if (codeLines.length > 0) {
const [, firstLinePath] = codeLines[0];
const codeBlock = editor.api.parent<TElement>(firstLinePath);
if (!codeBlock) return;
editor.tf.withoutNormalizing(() => {
for (const codeLine of codeLines) {
if (options.reverse) {
outdentCodeLine(editor, { codeBlock, codeLine });
} else {
indentCodeLine(editor, { codeBlock, codeLine });
}
}
});
return true; // Prevent default
}
};
if (apply()) return true;
return tab(options);
},
...withInsertDataCodeBlock(ctx).transforms,
...withInsertFragmentCodeBlock(ctx).transforms,
...withNormalizeCodeBlock(ctx).transforms,
},
};
};