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

Commit cca9d30

Browse files
Goamandmo-odoo
authored andcommitted
[FIX] Heading: restrict applyHeadingStyle
1 parent d689a44 commit cca9d30

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

packages/plugin-heading/src/Heading.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import { Parser } from '../../plugin-parser/src/Parser';
88
import { Keymap } from '../../plugin-keymap/src/Keymap';
99
import { CheckingContext } from '../../core/src/ContextManager';
1010
import { InsertParagraphBreakParams } from '../../core/src/Core';
11-
import { ContainerNode } from '../../core/src/VNodes/ContainerNode';
1211
import { Layout } from '../../plugin-layout/src/Layout';
1312
import { ActionableNode } from '../../plugin-layout/src/ActionableNode';
1413
import { Attributes } from '../../plugin-xml/src/Attributes';
1514
import { ComponentDefinition } from '../../plugin-layout/src/LayoutEngine';
1615
import { RuleProperty } from '../../core/src/Mode';
1716
import { isInTextualContext } from '../../utils/src/utils';
17+
import { ParagraphNode } from '../../plugin-paragraph/src/ParagraphNode';
18+
import { Paragraph } from '../../plugin-paragraph/src/Paragraph';
19+
import { Pre } from '../../plugin-pre/src/Pre';
20+
import { PreNode } from '../../plugin-pre/src/PreNode';
1821

1922
export interface HeadingParams extends CommandParams {
2023
level: number;
@@ -47,6 +50,7 @@ function headingButton(level: number): ComponentDefinition {
4750
}
4851

4952
export class Heading<T extends JWPluginConfig = JWPluginConfig> extends JWPlugin<T> {
53+
static dependencies = [Paragraph, Pre];
5054
commands = {
5155
applyHeadingStyle: {
5256
handler: this.applyHeadingStyle,
@@ -131,7 +135,12 @@ export class Heading<T extends JWPluginConfig = JWPluginConfig> extends JWPlugin
131135
* @param params
132136
*/
133137
applyHeadingStyle(params: HeadingParams): void {
134-
for (const node of params.context.range.targetedNodes(ContainerNode)) {
138+
for (const node of params.context.range.targetedNodes(
139+
node =>
140+
node instanceof HeadingNode ||
141+
node instanceof ParagraphNode ||
142+
node instanceof PreNode,
143+
)) {
135144
const heading = this._createHeadingContainer(params.level);
136145
heading.modifiers = node.modifiers.clone();
137146
node.replaceWith(heading);
@@ -147,7 +156,7 @@ export class Heading<T extends JWPluginConfig = JWPluginConfig> extends JWPlugin
147156
const range = params.context.range;
148157
const heading = range.targetedNodes(HeadingNode)[0];
149158
const duplicate = heading.splitAt(range.start);
150-
const newContainer = new this.editor.configuration.defaults.Container();
159+
const newContainer = new ParagraphNode();
151160
duplicate.replaceWith(newContainer);
152161
}
153162

@@ -162,7 +171,7 @@ export class Heading<T extends JWPluginConfig = JWPluginConfig> extends JWPlugin
162171
*/
163172
_createHeadingContainer(level: number): VNode {
164173
if (level === 0) {
165-
return new this.editor.configuration.defaults.Container();
174+
return new ParagraphNode();
166175
} else {
167176
return new HeadingNode({ level: level });
168177
}

packages/plugin-heading/test/Heading.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ describePlugin(Heading, testEditor => {
5252
contentAfter: '<p>[ab</p><h2>]cd</h2>',
5353
});
5454
});
55+
it('should not turn a div into a heading 0', async () => {
56+
await testEditor(BasicEditor, {
57+
contentBefore: '<div>[ab]</div>',
58+
stepFunction: applyHeadingStyle(0),
59+
contentAfter: '<div>[ab]</div>',
60+
});
61+
});
5562
});
5663
describe('level 1', () => {
5764
it('should turn a paragraph into a heading 1', async () => {
@@ -82,6 +89,13 @@ describePlugin(Heading, testEditor => {
8289
contentAfter: '<h1>[ab</h1><h2>]cd</h2>',
8390
});
8491
});
92+
it('should not turn a div into a heading 1', async () => {
93+
await testEditor(BasicEditor, {
94+
contentBefore: '<div>[ab]</div>',
95+
stepFunction: applyHeadingStyle(1),
96+
contentAfter: '<div>[ab]</div>',
97+
});
98+
});
8599
});
86100
describe('level 2', () => {
87101
it('should turn a heading 1 into a heading 2', async () => {
@@ -112,6 +126,13 @@ describePlugin(Heading, testEditor => {
112126
contentAfter: '<h2>[ab</h2><h1>]cd</h1>',
113127
});
114128
});
129+
it('should not turn a div into a heading 2', async () => {
130+
await testEditor(BasicEditor, {
131+
contentBefore: '<div>[ab]</div>',
132+
stepFunction: applyHeadingStyle(2),
133+
contentAfter: '<div>[ab]</div>',
134+
});
135+
});
115136
});
116137
describe('level 3', () => {
117138
it('should turn a heading 1 into a heading 3', async () => {
@@ -142,6 +163,13 @@ describePlugin(Heading, testEditor => {
142163
contentAfter: '<h3>[ab</h3><h1>]cd</h1>',
143164
});
144165
});
166+
it('should not turn a div into a heading 3', async () => {
167+
await testEditor(BasicEditor, {
168+
contentBefore: '<div>[ab]</div>',
169+
stepFunction: applyHeadingStyle(3),
170+
contentAfter: '<div>[ab]</div>',
171+
});
172+
});
145173
});
146174
describe('level 4', () => {
147175
it('should turn a heading 1 into a heading 4', async () => {
@@ -172,6 +200,13 @@ describePlugin(Heading, testEditor => {
172200
contentAfter: '<h4>[ab</h4><h1>]cd</h1>',
173201
});
174202
});
203+
it('should not turn a div into a heading 4', async () => {
204+
await testEditor(BasicEditor, {
205+
contentBefore: '<div>[ab]</div>',
206+
stepFunction: applyHeadingStyle(4),
207+
contentAfter: '<div>[ab]</div>',
208+
});
209+
});
175210
});
176211
describe('level 5', () => {
177212
it('should turn a heading 1 into a heading 5', async () => {
@@ -202,6 +237,13 @@ describePlugin(Heading, testEditor => {
202237
contentAfter: '<h5>[ab</h5><h1>]cd</h1>',
203238
});
204239
});
240+
it('should not turn a div into a heading 5', async () => {
241+
await testEditor(BasicEditor, {
242+
contentBefore: '<div>[ab]</div>',
243+
stepFunction: applyHeadingStyle(5),
244+
contentAfter: '<div>[ab]</div>',
245+
});
246+
});
205247
});
206248
describe('level 6', () => {
207249
it('should turn a heading 1 into a heading 6', async () => {
@@ -232,6 +274,13 @@ describePlugin(Heading, testEditor => {
232274
contentAfter: '<h6>[ab</h6><h1>]cd</h1>',
233275
});
234276
});
277+
it('should not turn a div into a heading 6', async () => {
278+
await testEditor(BasicEditor, {
279+
contentBefore: '<div>[ab]</div>',
280+
stepFunction: applyHeadingStyle(6),
281+
contentAfter: '<div>[ab]</div>',
282+
});
283+
});
235284
});
236285
});
237286
describe('insertParagraphBreak', () => {

0 commit comments

Comments
 (0)