Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 57 additions & 33 deletions src/tinymce.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Base from "@patternslib/patternslib/src/core/base";
import { BasePattern } from "@patternslib/patternslib/src/core/basepattern";
import Parser from "@patternslib/patternslib/src/core/parser";
import registry from "@patternslib/patternslib/src/core/registry";

const plugin_map = {
advlist: { deps: ["lists"] },
Expand Down Expand Up @@ -46,14 +47,16 @@ parser.addArgument("content-css", false);
parser.addArgument("license-key", "gpl");
parser.add_argument("plugins", Object.keys(plugin_map), Object.keys(plugin_map), true);

export default Base.extend({
name: "tinymce",
trigger: ".pat-tinymce",
plugins: {},
class Pattern extends BasePattern {
static name = "tinymce";
static trigger = ".pat-tinymce";
static parser = parser;

async init() {
this.options = parser.parse(this.el, this.options);
plugins = {};
tinyel = null;
tinymce = null;

async init() {
const TinyMCE = (await import("tinymce")).default;

// UI, design, skin
Expand All @@ -73,18 +76,6 @@ export default Base.extend({
content_css_cors = true;
}

this.tinyel = this.el;
if (
this.options.inline &&
["textarea", "input"].includes(this.el.nodeName.toLowerCase())
) {
this.tinyel = document.createElement("div");
this.tinyel.setAttribute("contenteditable", "");
this.tinyel.innerHTML = this.el.value;
this.el.parentNode.insertBefore(this.tinyel, this.el);
this.el.setAttribute("hidden", "");
}

// load plugins
for (const plugin of this.options.plugins) {
this.plugins[plugin] = await import(`tinymce/plugins/${plugin}`);
Expand All @@ -103,6 +94,23 @@ export default Base.extend({
}
}

if (this.destroyed) {
// Destroyed while loading. Don't create the editor.
return;
}

this.tinyel = this.el;
if (
this.options.inline &&
["textarea", "input"].includes(this.el.nodeName.toLowerCase())
) {
this.tinyel = document.createElement("div");
this.tinyel.setAttribute("contenteditable", "");
this.tinyel.innerHTML = this.el.value;
this.el.parentNode.insertBefore(this.tinyel, this.el);
this.el.setAttribute("hidden", "");
}

const config = {
target: this.tinyel,
license_key: this.options.licenseKey,
Expand All @@ -117,6 +125,12 @@ export default Base.extend({
// apparently it's returning the editor wrapped in a list
this.tinymce = this.tinymce[0];

if (this.destroyed) {
// Destroyed while the editor was initializing.
this.destroy_editor();
return;
}

if (this.tinyel !== this.el) {
// Update <textarea> or <input>, if one of these were the
// initializing elements.
Expand All @@ -127,19 +141,29 @@ export default Base.extend({
this.el.value = this.tinyel.innerHTML;
});
}
},
}

destroy() {
if (this.tinymce) {
if (this.tinyel !== this.el) {
// destroy also inline editable
// save back first.
this.el.value = this.tinyel.innerHTML;
this.tinyel.remove();
this.el.removeAttribute("hidden");
}
this.tinymce.destroy();
this.tinymce = null;
destroy_editor() {
if (!this.tinymce) {
return;
}
},
});
if (this.tinyel !== this.el) {
// destroy also inline editable
// save back first.
this.el.value = this.tinyel.innerHTML;
this.tinyel.remove();
this.el.removeAttribute("hidden");
}
this.tinymce.destroy();
this.tinymce = null;
}

destroy() {
this.destroy_editor();
super.destroy();
}
}

registry.register(Pattern);
export default Pattern;
export { Pattern };
121 changes: 112 additions & 9 deletions src/tinymce.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pattern from "./tinymce";
import Pattern from "./tinymce";
import events from "@patternslib/patternslib/src/core/events";
import utils from "@patternslib/patternslib/src/core/utils";

// jsdom does not implement CSS.escape, which TinyMCE uses for schema selectors.
Expand All @@ -11,6 +12,14 @@ window.CSS = {
),
};

// Resolves when the pattern was initialized or not initialized. Patternslib
// 9.12 does not initialize patterns which were destroyed before init.
const init_done = (el) =>
Promise.race([
events.await_event(el, "init.tinymce.patterns"),
events.await_event(el, "not-init.tinymce.patterns"),
]);

describe("pat-tinymce", () => {
afterEach(() => {
window.tinymce?.remove();
Expand All @@ -29,8 +38,12 @@ describe("pat-tinymce", () => {
data-pat-tinymce="inline: false">${text_1}</textarea>
`;

pattern.init(document.querySelector(".pat-tinymce"));
await utils.timeout(1);
new Pattern(document.querySelector(".pat-tinymce"));
// In iframe mode, the TinyMCE initialization does not finish in jsdom.
// Wait for the editor DOM instead of the pattern initialization.
while (!document.querySelector("iframe")) {
await utils.timeout(10);
}

// Textarea is hidden
expect(
Expand All @@ -54,8 +67,8 @@ describe("pat-tinymce", () => {
data-pat-tinymce="inline: true;">${text_1}</textarea>
`;

pattern.init(document.querySelector(".pat-tinymce"));
await utils.timeout(1);
const instance = new Pattern(document.querySelector(".pat-tinymce"));
await events.await_pattern_init(instance);

const textarea = document.querySelector("textarea");
const tiny_el = document.querySelector("div[contenteditable]");
Expand Down Expand Up @@ -84,10 +97,14 @@ describe("pat-tinymce", () => {
data-pat-tinymce="inline: true;">${text_3}</textarea>
`;

pattern.init(document.querySelectorAll(".pat-tinymce")[0]);
await utils.timeout(1);
pattern.init(document.querySelectorAll(".pat-tinymce")[1]);
await utils.timeout(1);
const instance_1 = new Pattern(document.querySelectorAll(".pat-tinymce")[0]);
await events.await_pattern_init(instance_1);
const instance_2 = new Pattern(document.querySelectorAll(".pat-tinymce")[1]);
await events.await_pattern_init(instance_2);

// The instances do not share state.
expect(instance_1.tinymce).not.toBe(instance_2.tinymce);
expect(instance_1.plugins).not.toBe(instance_2.plugins);

const textarea_1 = document.querySelectorAll("textarea.pat-tinymce")[0];
const tiny_el_1 = document.querySelectorAll("div[contenteditable]")[0];
Expand All @@ -108,4 +125,90 @@ describe("pat-tinymce", () => {
expect(textarea_1.value).toBe(text_1 + text_2);
expect(textarea_2.value).toBe(text_3 + text_4);
});

it("Restores the textarea in inline mode on destroy", async () => {
const text_1 = "<h1>first</h1>";
const text_2 = "<h2>second</h2>";

document.body.innerHTML = `
<textarea
class="pat-tinymce"
data-pat-tinymce="inline: true;">${text_1}</textarea>
`;

const textarea = document.querySelector("textarea");
const instance = new Pattern(textarea);
await events.await_pattern_init(instance);

const editor = instance.tinymce;
const tiny_el = document.querySelector("div[contenteditable]");
expect(textarea.hasAttribute("hidden")).toBe(true);

// Changes which were not yet synced are saved back on destroy.
tiny_el.innerHTML += text_2;

instance.destroy();

expect(instance.tinymce).toBe(null);
expect(editor.removed).toBe(true);
expect(tiny_el.isConnected).toBe(false);
expect(textarea.hasAttribute("hidden")).toBe(false);
expect(textarea.value).toBe(text_1 + text_2);
expect(textarea["pattern-tinymce"]).toBeUndefined();
});

it("Can be destroyed before the editor was initialized", async () => {
document.body.innerHTML = `<div class="pat-tinymce"></div>`;
const el = document.querySelector(".pat-tinymce");
const done = init_done(el);
const instance = new Pattern(el);
instance.destroy();
expect(instance.tinymce).toBe(null);
await done;
instance.destroy();
expect(el["pattern-tinymce"]).toBeUndefined();
});

it("Does not create the editor when destroyed while loading", async () => {
document.body.innerHTML = `
<textarea class="pat-tinymce" data-pat-tinymce="inline: true;">text</textarea>
`;
const textarea = document.querySelector("textarea");
const done = init_done(textarea);
const instance = new Pattern(textarea);
// Emulate a destroy while the dependencies are loading. BasePattern
// sets this flag in destroy() since Patternslib 9.12.
instance.destroyed = true;
await done;

expect(instance.tinymce).toBeFalsy();
expect(document.querySelector("div[contenteditable]")).toBe(null);
expect(textarea.hasAttribute("hidden")).toBe(false);
});

it("Destroys the editor when destroyed while the editor was initializing", async () => {
const TinyMCE = (await import("tinymce")).default;
const original_init = TinyMCE.init.bind(TinyMCE);
let instance;
const spy = jest.spyOn(TinyMCE, "init").mockImplementation(async (config) => {
const editors = await original_init(config);
// Emulate a destroy while the editor was initializing.
instance.destroyed = true;
return editors;
});

document.body.innerHTML = `
<textarea class="pat-tinymce" data-pat-tinymce="inline: true;">text</textarea>
`;
const textarea = document.querySelector("textarea");
instance = new Pattern(textarea);
await events.await_pattern_init(instance);

expect(spy).toHaveBeenCalled();
expect(instance.tinymce).toBe(null);
expect(window.tinymce.get().length).toBe(0);
expect(document.querySelector("div[contenteditable]")).toBe(null);
expect(textarea.hasAttribute("hidden")).toBe(false);
spy.mockRestore();
});
});