Skip to content

Commit d571d00

Browse files
committed
refactor(all): code optimization
1 parent 5ee5ccb commit d571d00

File tree

81 files changed

+1609
-1325
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+1609
-1325
lines changed

examples/react/components/editor/plugins/test/index.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,19 @@ export default class extends Plugin<Options> {
2222
}
2323
// 插件初始化
2424
init() {
25+
const editor = this.editor;
2526
// 监听解析成html的事件
26-
this.editor.on('parse:html', this.parseHtml);
27+
editor.on('parse:html', this.parseHtml);
2728
// 监听粘贴时候设置schema规则的入口
28-
this.editor.on('paste:schema', this.pasteSchema);
29+
editor.on('paste:schema', this.pasteSchema);
2930
// 监听粘贴时候的节点循环
30-
this.editor.on('paste:each', this.pasteHtml);
31+
editor.on('paste:each', this.pasteHtml);
3132
}
3233
// 执行方法
3334
execute() {
34-
if (!isEngine(this.editor) || this.editor.readonly) return;
35-
const { card } = this.editor;
35+
const editor = this.editor;
36+
if (!isEngine(editor) || editor.readonly) return;
37+
const { card } = editor;
3638
card.insert<TestValue>(TestComponent.cardName, {
3739
text: 'This is card value',
3840
});
@@ -57,13 +59,14 @@ export default class extends Plugin<Options> {
5759
};
5860
// 解析粘贴过来的html
5961
pasteHtml = (node: NodeInterface) => {
60-
if (!isEngine(this.editor) || this.editor.readonly) return;
62+
const editor = this.editor;
63+
if (!isEngine(editor) || editor.readonly) return;
6164
if (node.isElement()) {
6265
const type = node.attributes('data-type');
6366
if (type === TestComponent.cardName) {
6467
const value = node.attributes('data-value');
6568
const cardValue = decodeCardValue(value);
66-
this.editor.card.replaceNode(
69+
editor.card.replaceNode(
6770
node,
6871
TestComponent.cardName,
6972
cardValue,
@@ -97,12 +100,13 @@ export default class extends Plugin<Options> {
97100
};
98101

99102
destroy() {
103+
const editor = this.editor;
100104
// 监听解析成html的事件
101-
this.editor.off('parse:html', this.parseHtml);
105+
editor.off('parse:html', this.parseHtml);
102106
// 监听粘贴时候设置schema规则的入口
103-
this.editor.off('paste:schema', this.pasteSchema);
107+
editor.off('paste:schema', this.pasteSchema);
104108
// 监听粘贴时候的节点循环
105-
this.editor.off('paste:each', this.pasteHtml);
109+
editor.off('paste:each', this.pasteHtml);
106110
}
107111
}
108112
export { TestComponent };

packages/engine/src/block/index.ts

+55-43
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class Block implements BlockModelInterface {
6262
const editor = this.editor;
6363
if (!isEngine(editor) || editor.options.markdown?.mode === false)
6464
return;
65-
const { change, block } = editor;
65+
const { change } = editor;
6666
let range = change.range.get();
6767
if (!range.collapsed || change.isComposing()) return;
6868
const { startNode, startOffset } = range;
@@ -75,10 +75,10 @@ class Block implements BlockModelInterface {
7575
if (!editor.node.isRootBlock(blockNode)) return;
7676
const text = node.text().trimEnd();
7777
const cacheRange = range.toPath();
78-
const markdown = createMarkdownIt(this.editor, 'zero');
78+
const markdown = createMarkdownIt(editor, 'zero');
7979
const tokens = markdown.parse(text, {});
8080
if (tokens.length === 0) return;
81-
const content = convertMarkdown(this.editor, markdown, tokens, false);
81+
const content = convertMarkdown(editor, markdown, tokens, false);
8282
if (content) {
8383
event.preventDefault();
8484
range.select(blockNode, true);
@@ -182,8 +182,9 @@ class Block implements BlockModelInterface {
182182
* @param range 光标
183183
*/
184184
wrap(block: NodeInterface | Node | string, range?: RangeInterface) {
185-
if (!isEngine(this.editor)) return;
186-
const { change, node, schema, list, mark } = this.editor;
185+
const editor = this.editor;
186+
if (!isEngine(editor)) return;
187+
const { change, node, schema, list, mark } = editor;
187188
const safeRange = range || change.range.toTrusty();
188189
const doc = getDocument(safeRange.startContainer);
189190
if (typeof block === 'string' || isNode(block)) {
@@ -272,8 +273,9 @@ class Block implements BlockModelInterface {
272273
* @param range 光标
273274
*/
274275
unwrap(block: NodeInterface | Node | string, range?: RangeInterface) {
275-
if (!isEngine(this.editor)) return;
276-
const { change, node } = this.editor;
276+
const editor = this.editor;
277+
if (!isEngine(editor)) return;
278+
const { change, node } = editor;
277279
const safeRange = range || change.range.toTrusty();
278280
const doc = getDocument(safeRange.startContainer);
279281
if (typeof block === 'string' || isNode(block)) {
@@ -409,8 +411,9 @@ class Block implements BlockModelInterface {
409411
* @param range 光标
410412
*/
411413
split(range?: RangeInterface) {
412-
if (!isEngine(this.editor)) return;
413-
const { change, mark, nodeId } = this.editor;
414+
const editor = this.editor;
415+
if (!isEngine(editor)) return;
416+
const { change, mark, nodeId } = editor;
414417
const safeRange = range || change.range.toTrusty();
415418
// 范围为展开状态时先删除内容
416419
if (!safeRange.collapsed) {
@@ -441,7 +444,7 @@ class Block implements BlockModelInterface {
441444
cloneRange.shrinkToElementNode().shrinkToTextNode().collapse(true);
442445
const activeMarks = mark.findMarks(cloneRange).filter((mark) => {
443446
// 回车后,默认是否复制makr样式
444-
const plugin = this.editor.mark.findPlugin(mark);
447+
const plugin = editor.mark.findPlugin(mark);
445448
return (
446449
plugin?.copyOnEnter !== false && plugin?.followStyle !== false
447450
);
@@ -453,7 +456,7 @@ class Block implements BlockModelInterface {
453456
isLeft: false,
454457
keepDataId: true,
455458
});
456-
const nodeApi = this.editor.node;
459+
const nodeApi = editor.node;
457460
sideBlock.traverse((node) => {
458461
if (
459462
!nodeApi.isVoid(node) &&
@@ -498,12 +501,12 @@ class Block implements BlockModelInterface {
498501
}
499502
block.children().each((child) => {
500503
if (nodeApi.isInline(child)) {
501-
this.editor.inline.repairCursor(child);
504+
editor.inline.repairCursor(child);
502505
}
503506
});
504507
sideBlock.children().each((child) => {
505508
if (nodeApi.isInline(child)) {
506-
this.editor.inline.repairCursor(child);
509+
editor.inline.repairCursor(child);
507510
}
508511
});
509512
// 重新设置当前选中范围
@@ -533,8 +536,9 @@ class Block implements BlockModelInterface {
533536
splitNode?: (node: NodeInterface) => NodeInterface,
534537
removeCurrentEmptyBlock: boolean = false,
535538
) {
536-
if (!isEngine(this.editor)) return;
537-
const { change, node, list, inline } = this.editor;
539+
const editor = this.editor;
540+
if (!isEngine(editor)) return;
541+
const { change, node, list, inline } = editor;
538542
const safeRange = range || change.range.toTrusty();
539543
const doc = getDocument(safeRange.startContainer);
540544
if (typeof block === 'string' || isNode(block)) {
@@ -640,11 +644,11 @@ class Block implements BlockModelInterface {
640644
!list.isEmptyItem(rightNodes)
641645
) {
642646
const right = rightNodes.clone(false);
643-
this.editor.nodeId.generate(right, true);
647+
editor.nodeId.generate(right, true);
644648
const rightChildren = rightNodes.children();
645649
rightChildren.each((child, index) => {
646650
if (rightChildren.eq(index)?.isCard()) {
647-
const card = this.editor.card.find(child);
651+
const card = editor.card.find(child);
648652
if (card) right.append(card.root);
649653
} else right.append(child);
650654
});
@@ -698,9 +702,10 @@ class Block implements BlockModelInterface {
698702
* @param range 光标
699703
*/
700704
setBlocks(block: string | { [k: string]: any }, range?: RangeInterface) {
701-
if (!isEngine(this.editor)) return;
702-
const { node, schema, mark } = this.editor;
703-
const { change } = this.editor;
705+
const editor = this.editor;
706+
if (!isEngine(editor)) return;
707+
const { node, schema, mark } = editor;
708+
const { change } = editor;
704709
const safeRange = range || change.range.toTrusty();
705710
const doc = getDocument(safeRange.startContainer);
706711
let targetNode: NodeInterface | null = null;
@@ -828,8 +833,9 @@ class Block implements BlockModelInterface {
828833
* @param range 光标
829834
*/
830835
merge(range?: RangeInterface) {
831-
if (!isEngine(this.editor)) return;
832-
const { change, schema } = this.editor;
836+
const editor = this.editor;
837+
if (!isEngine(editor)) return;
838+
const { change, schema } = editor;
833839
const safeRange = range || change.range.toTrusty();
834840
const blocks = this.getBlocks(safeRange);
835841
if (0 === blocks.length) return;
@@ -853,7 +859,7 @@ class Block implements BlockModelInterface {
853859
Object.keys(nextAttributes).join(',') ===
854860
Object.keys(prevAttributes || {}).join(',')
855861
) {
856-
this.editor.node.merge(prevNode, nextNode);
862+
editor.node.merge(prevNode, nextNode);
857863
}
858864
nextNode = nextNode.next();
859865
}
@@ -866,11 +872,12 @@ class Block implements BlockModelInterface {
866872
* 获取对范围有效果的所有 Block
867873
*/
868874
findBlocks(range: RangeInterface) {
875+
const editor = this.editor;
869876
range = range.cloneRange();
870877
if (range.startNode.isRoot()) range.shrinkToElementNode();
871878
if (
872879
!range.startNode.inEditor() ||
873-
this.editor.card.find(range.startNode)?.type === CardType.BLOCK
880+
editor.card.find(range.startNode)?.type === CardType.BLOCK
874881
)
875882
return [];
876883
const sc = range.startContainer;
@@ -920,7 +927,7 @@ class Block implements BlockModelInterface {
920927
if (node.isEditable()) {
921928
break;
922929
}
923-
if (this.editor.node.isBlock(node)) {
930+
if (editor.node.isBlock(node)) {
924931
nodes.push(node);
925932
}
926933
const parent = node.parent();
@@ -936,7 +943,7 @@ class Block implements BlockModelInterface {
936943
return addNode(nodes, node, true);
937944
});
938945
const { commonAncestorNode } = range;
939-
const card = this.editor.card.find(commonAncestorNode, true);
946+
const card = editor.card.find(commonAncestorNode, true);
940947
let isEditable = card?.isEditable;
941948
const selectionNodes = isEditable
942949
? card?.getSelectionNodes
@@ -956,7 +963,7 @@ class Block implements BlockModelInterface {
956963
if (
957964
child.isElement() &&
958965
!child.isCard() &&
959-
this.editor.node.isBlock(child)
966+
editor.node.isBlock(child)
960967
) {
961968
addNode(nodes, child);
962969
}
@@ -982,13 +989,14 @@ class Block implements BlockModelInterface {
982989
const block = this.closest(container);
983990
range.select(block, true);
984991
range.setEnd(container[0], offset);
985-
if (!this.editor.node.isBlock(container)) range.enlargeToElementNode();
992+
const editor = this.editor;
993+
if (!editor.node.isBlock(container)) range.enlargeToElementNode();
986994
const fragment = range.cloneContents();
987995

988996
if (!fragment.firstChild) {
989997
return true;
990998
}
991-
const { node } = this.editor;
999+
const { node } = editor;
9921000
if (
9931001
fragment.childNodes.length === 1 &&
9941002
$(fragment.firstChild).name === 'br'
@@ -1014,13 +1022,13 @@ class Block implements BlockModelInterface {
10141022
const block = this.closest(container);
10151023
range.select(block, true);
10161024
range.setStart(container, offset);
1017-
if (!this.editor.node.isBlock(container)) range.enlargeToElementNode();
1025+
const { node } = this.editor;
1026+
if (!node.isBlock(container)) range.enlargeToElementNode();
10181027
const fragment = range.cloneContents();
10191028

10201029
if (!fragment.firstChild) {
10211030
return true;
10221031
}
1023-
const { node } = this.editor;
10241032
const emptyNode = $('<div />');
10251033
emptyNode.append(fragment);
10261034

@@ -1035,8 +1043,8 @@ class Block implements BlockModelInterface {
10351043
range = range.cloneRange();
10361044
range.shrinkToElementNode();
10371045
range.shrinkToTextNode();
1038-
1039-
const { node } = this.editor;
1046+
const editor = this.editor;
1047+
const { node } = editor;
10401048

10411049
let startBlock = this.closest(range.startNode);
10421050
if (range.startNode.isRoot()) {
@@ -1051,7 +1059,7 @@ class Block implements BlockModelInterface {
10511059
const blocks: Array<NodeInterface> = [];
10521060
let started = false;
10531061
const { commonAncestorNode } = range;
1054-
const card = this.editor.card.find(commonAncestorNode, true);
1062+
const card = editor.card.find(commonAncestorNode, true);
10551063
let isEditable = card?.isEditable;
10561064
const selectionNodes = isEditable
10571065
? card?.getSelectionNodes
@@ -1071,7 +1079,7 @@ class Block implements BlockModelInterface {
10711079
}
10721080
if (
10731081
(started || isEditable) &&
1074-
this.editor.node.isBlock(child) &&
1082+
editor.node.isBlock(child) &&
10751083
!child.isCard() &&
10761084
child.inEditor()
10771085
) {
@@ -1119,7 +1127,8 @@ class Block implements BlockModelInterface {
11191127
keepDataId?: boolean;
11201128
}) {
11211129
if (isNode(block)) block = $(block);
1122-
const newRange = Range.create(this.editor, block.document!);
1130+
const editor = this.editor;
1131+
const newRange = Range.create(editor, block.document!);
11231132

11241133
if (isLeft) {
11251134
newRange.select(block, true);
@@ -1134,7 +1143,7 @@ class Block implements BlockModelInterface {
11341143
: newRange.extractContents();
11351144
const cloneBlock = keepDataId
11361145
? block.clone(false)
1137-
: this.editor.node.clone(block, false, false);
1146+
: editor.node.clone(block, false, false);
11381147
cloneBlock.append(fragement);
11391148
if (clone) {
11401149
cloneBlock.find(CARD_SELECTOR).each((card) => {
@@ -1152,8 +1161,9 @@ class Block implements BlockModelInterface {
11521161
* @param block 节点
11531162
*/
11541163
getLeftText(block: NodeInterface | Node, range?: RangeInterface) {
1155-
if (!isEngine(this.editor)) return '';
1156-
range = range || this.editor.change.range.get();
1164+
const editor = this.editor;
1165+
if (!isEngine(editor)) return '';
1166+
range = range || editor.change.range.get();
11571167
const leftBlock = this.getBlockByRange({
11581168
block,
11591169
range,
@@ -1168,8 +1178,9 @@ class Block implements BlockModelInterface {
11681178
* @param block 节点
11691179
*/
11701180
removeLeftText(block: NodeInterface | Node, range?: RangeInterface) {
1171-
if (!isEngine(this.editor)) return;
1172-
range = range || this.editor.change.range.get();
1181+
const editor = this.editor;
1182+
if (!isEngine(editor)) return;
1183+
range = range || editor.change.range.get();
11731184
if (isNode(block)) block = $(block);
11741185
range.createSelection();
11751186
const cursor = block.find(CURSOR_SELECTOR);
@@ -1194,8 +1205,9 @@ class Block implements BlockModelInterface {
11941205
* @param root 根节点
11951206
*/
11961207
flat(block: NodeInterface, root: NodeInterface) {
1197-
if (!isEngine(this.editor)) return;
1198-
const { schema, node } = this.editor;
1208+
const editor = this.editor;
1209+
if (!isEngine(editor)) return;
1210+
const { schema, node } = editor;
11991211
const mergeTags = schema.getCanMergeTags();
12001212
//获取父级节点
12011213
let parentNode = block.parent();

0 commit comments

Comments
 (0)