Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit b150368

Browse files
committed
perf: inline getCurrentStyle => change selection, insert new char
1 parent 04fcb7e commit b150368

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

packages/plugin-char/src/Char.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,13 @@ export class Char<T extends JWPluginConfig = JWPluginConfig> extends JWPlugin<T>
5050
let modifiers = inline.getCurrentModifiers(range);
5151
// Ony preserved modifiers are applied at the start of a container.
5252
const previousSibling = range.start.previousSibling();
53-
if (!previousSibling) {
53+
if (!previousSibling && modifiers) {
5454
modifiers = new Modifiers(...modifiers.filter(mod => mod.preserve));
5555
}
5656
if (params.formats) {
57+
if (!modifiers) {
58+
modifiers = new Modifiers();
59+
}
5760
modifiers.set(...params.formats.map(format => format.clone()));
5861
}
5962
const style = inline.getCurrentStyle(range);
@@ -64,17 +67,20 @@ export class Char<T extends JWPluginConfig = JWPluginConfig> extends JWPlugin<T>
6467
// Split the text into CHAR nodes and insert them at the range.
6568
const characters = text.split('');
6669
const charNodes = characters.map(char => {
67-
return new CharNode({ char: char, modifiers: modifiers.clone() });
70+
if (modifiers) {
71+
return new CharNode({ char: char, modifiers: modifiers.clone() });
72+
} else {
73+
return new CharNode({ char: char });
74+
}
6875
});
6976
charNodes.forEach(charNode => {
70-
if (style.length) {
77+
if (style?.length) {
7178
charNode.modifiers.get(Attributes).style = style;
7279
}
7380
range.start.before(charNode);
7481
});
7582
if (params.select && charNodes.length) {
7683
this.editor.selection.select(charNodes[0], charNodes[charNodes.length - 1]);
7784
}
78-
inline.resetCache();
7985
}
8086
}

packages/plugin-inline/src/Inline.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export class Inline<T extends JWPluginConfig = JWPluginConfig> extends JWPlugin<
125125
isAllFormat(FormatClass: Constructor<Modifier>, range = this.editor.selection.range): boolean {
126126
if (range.isCollapsed()) {
127127
if (!this.cache.modifiers) {
128-
return !!this.getCurrentModifiers(range).find(FormatClass);
128+
return !!this.getCurrentModifiers(range)?.find(FormatClass);
129129
}
130130
return !!this.cache.modifiers.find(FormatClass);
131131
} else {
@@ -139,30 +139,44 @@ export class Inline<T extends JWPluginConfig = JWPluginConfig> extends JWPlugin<
139139
/**
140140
* Get the modifiers for the next insertion.
141141
*/
142-
getCurrentModifiers(range = this.editor.selection.range): Modifiers {
143-
if (this.cache.modifiers) {
142+
getCurrentModifiers(range = this.editor.selection.range): Modifiers | null {
143+
if (this.cache.modifiers && range === this.editor.selection.range) {
144144
return this.cache.modifiers;
145+
} else {
146+
return this._getCurrentModifiers(range);
145147
}
146-
148+
}
149+
/**
150+
* Get the current modifiers.
151+
*/
152+
private _getCurrentModifiers(range = this.editor.selection.range): Modifiers | null {
147153
let inlineToCopyModifiers: VNode;
148154
if (range.isCollapsed()) {
149155
// TODO: LineBreakNode should have the formats as well.
150156
inlineToCopyModifiers =
151-
range.start.previousSibling(node => !node.test(LineBreakNode)) ||
157+
range.start.previousSibling(node => !(node instanceof LineBreakNode)) ||
152158
range.start.nextSibling();
153159
} else {
154160
inlineToCopyModifiers = range.start.nextSibling();
155161
}
156162
if (inlineToCopyModifiers && inlineToCopyModifiers.is(InlineNode)) {
157163
return inlineToCopyModifiers.modifiers.clone();
158164
}
159-
160-
return new Modifiers();
161165
}
162166
/**
163167
* Get the styles for the next insertion.
164168
*/
165-
getCurrentStyle(range = this.editor.selection.range): CssStyle {
169+
getCurrentStyle(range = this.editor.selection.range): CssStyle | null {
170+
if (this.cache.style && range === this.editor.selection.range) {
171+
return this.cache.style;
172+
} else {
173+
return this._getCurrentStyle(range);
174+
}
175+
}
176+
/**
177+
* Get the current styles for the next insertion.
178+
*/
179+
private _getCurrentStyle(range = this.editor.selection.range): CssStyle | null {
166180
if (this.cache.style) {
167181
return this.cache.style;
168182
}
@@ -176,15 +190,14 @@ export class Inline<T extends JWPluginConfig = JWPluginConfig> extends JWPlugin<
176190
if (inlineToCopyStyle && inlineToCopyStyle.is(InlineNode)) {
177191
return inlineToCopyStyle.modifiers.find(Attributes)?.style.clone() || new CssStyle();
178192
}
179-
return new CssStyle();
180193
}
181194
/**
182195
* Each time the selection changes, we reset its format and style.
183196
*/
184197
resetCache(): void {
185198
this.cache = {
186-
modifiers: null,
187-
style: null,
199+
modifiers: this._getCurrentModifiers(),
200+
style: this._getCurrentStyle(),
188201
};
189202
}
190203
/**

0 commit comments

Comments
 (0)