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

Commit

Permalink
[FIX] format
Browse files Browse the repository at this point in the history
  • Loading branch information
Goaman committed May 28, 2020
1 parent a3f4c17 commit f37a265
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
20 changes: 20 additions & 0 deletions packages/core/src/Modifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ export class Modifiers {
}
return found;
}
/**
* Return all the modifiers instances that are hold by `this`.
*/
getContent(): Modifier[] {
return this._contents;
}
/**
* Return all modifiers in the array that are an instance of the given
* modifier class, if any.
Expand Down Expand Up @@ -198,6 +204,20 @@ export class Modifiers {
return true;
}
}
/**
* Replace the modifiers that have the same constructor. Otherwise append
* them.
*/
replaceOrAppend(modifiers: Modifiers): void {
for (const modifier of modifiers.getContent()) {
const foundModifier = this.find(modifier.constructor);
if (foundModifier) {
this.replace(foundModifier, modifier);
} else {
this.append(modifier.clone());
}
}
}
/**
* Remove the first modifier in the array that is an instance of the given
* modifier class or that matches the particular instance passed.
Expand Down
41 changes: 41 additions & 0 deletions packages/core/test/modifiers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Modifiers } from '../src/Modifiers';
import { Modifier } from '../src/Modifier';
import { expect } from 'chai';

function id<T>(x: T): T {
return x;
}
describe('core', () => {
describe('Modifiers', () => {
describe('prepend()', () => {
it('should prepend multiples modifier in proper order', () => {
const modifiers = new Modifiers();
const m1 = new Modifier();
const m2 = new Modifier();
const m3 = new Modifier();
const m4 = new Modifier();
modifiers.prepend(m1, m2, m3, m4);
const modifiersMap = modifiers.map(id);
expect(modifiersMap[0]).to.equal(m1);
expect(modifiersMap[1]).to.equal(m2);
expect(modifiersMap[2]).to.equal(m3);
expect(modifiersMap[3]).to.equal(m4);
});
});
describe('replaceOrAppend()', () => {
it('', () => {
class Modifier1 extends Modifier {}
class Modifier2 extends Modifier {}
const m1a = new Modifier1();
const m1b = new Modifier1();
const m2 = new Modifier2();
const modifiers1 = new Modifiers(m1a, m2);
modifiers1.replaceOrAppend(new Modifiers(m1b));
const allModifiers = modifiers1.getContent();
expect(allModifiers.length).to.eql(2);
expect(allModifiers[0]).to.eql(m1b);
expect(allModifiers[1]).to.eql(m2);
});
});
});
});
2 changes: 1 addition & 1 deletion packages/plugin-char/src/Char.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class Char<T extends JWPluginConfig = JWPluginConfig> extends JWPlugin<T>
const inline = this.editor.plugins.get(Inline);
const modifiers = inline.getCurrentModifiers(range);
if (params.formats) {
modifiers.append(...params.formats.map(format => format.clone()));
modifiers.replaceOrAppend(params.formats);
}
const style = inline.getCurrentStyle(range);
// Remove the contents of the range if needed.
Expand Down
9 changes: 9 additions & 0 deletions packages/plugin-link/src/LinkFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@ export class LinkFormat extends Format {
element.setAttribute('href', this.url);
return element;
}

/**
* @override
*/
clone(): this {
const clone = super.clone();
clone.url = this.url;
return clone;
}
}
12 changes: 12 additions & 0 deletions packages/plugin-link/test/LinkFormat.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { LinkFormat } from '../src/LinkFormat';
import { expect } from 'chai';
describe('Link', () => {
describe('LinkFormat', () => {
describe('clone()', () => {
it('should clone the link with proper url', () => {
const format = new LinkFormat('/url');
expect(format.clone().url).to.eql('/url');
});
});
});
});

0 comments on commit f37a265

Please sign in to comment.