Skip to content

Commit

Permalink
Replace activateEditor Wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
mclemente committed Oct 6, 2023
1 parent 052c33c commit 459dae8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 64 deletions.
1 change: 1 addition & 0 deletions src/module/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export default class PolyglotHooks {
* @param {*} html
*/
static renderJournalSheet(journalSheet, html) {
CONFIG.TinyMCE.style_formats.find((f) => f.title === "Polyglot").items = game.polyglot.getLanguagesForEditor();
if (journalSheet.document?.isOwner || game.user.isGM) {
const toggleButton = game.polyglot.createJournalButton(journalSheet);
html.closest(".app").find(".polyglot-button").remove();
Expand Down
96 changes: 32 additions & 64 deletions src/module/logic.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-undef */
import { FONTS } from "./Fonts.js";
import PolyglotHooks from "./hooks.js";

Expand All @@ -20,16 +19,9 @@ export class Polyglot {
Hooks.on(hook, PolyglotHooks[hook]);
}
}
libWrapper.register("polyglot", "JournalTextPageSheet.prototype.activateEditor", this.activateEditorWrapper.bind(this), "WRAPPER");
/**
* Speak a message as a particular Token, displaying it as a chat bubble
* WRAPPER:
* Scrambles the message's text if a language is present.
* @param {Token} token The speaking Token
* @param {string} message The spoken message text
* @param {ChatBubbleOptions} [options] Options which affect the bubble appearance
* @returns {Promise<jQuery|null>} A Promise which resolves to the created bubble HTML element, or null
*/
Polyglot.handleTinyMCE();

// eslint-disable-next-line no-undef
libWrapper.register(
"polyglot",
"ChatBubbles.prototype.say",
Expand Down Expand Up @@ -565,16 +557,36 @@ export class Polyglot {
}

/* -------------------------------------------- */
/* Wrappers */
/* Journal Editor */
/* -------------------------------------------- */

activateEditorWrapper(wrapped, target, editorOptions, initialContent) {
// let { target, editorOptions, initialContent } = activeEditorLogic(target, editorOptions, initialContent);
this.activeEditorLogic(editorOptions);
return wrapped(target, editorOptions, initialContent);
static handleTinyMCE() {
// Add Polyglot to TinyMCE's menu
CONFIG.TinyMCE.style_formats.push({
title: "Polyglot",
items: {},
});
// Add custom config to remove spans from polyglot when needed
const removeFormat = [
{
selector: "span",
classes: "polyglot-journal",
attributes: ["title", "class", "data-language"],
remove: "all",
split: true,
expand: false,
deep: true,
},
];
if (!CONFIG.TinyMCE.formats) {
CONFIG.TinyMCE.formats = {
removeformat: removeFormat,
};
} else if (!CONFIG.TinyMCE.formats.removeformat) CONFIG.TinyMCE.formats.removeformat = [...removeFormat];
else CONFIG.TinyMCE.formats.removeformat.push(...removeFormat);
}

activeEditorLogic(editorOptions = {}) {
getLanguagesForEditor() {
let langs = this.languageProvider.languages;
if (!game.user.isGM) {
langs = {};
Expand Down Expand Up @@ -605,58 +617,14 @@ export class Polyglot {
};
});
if (this.truespeech) {
const truespeechIndex = languages.findIndex((element) => element.attributes["data-language"] == this.truespeech);
const truespeechIndex = languages.findIndex((element) => element.attributes["data-language"] === this.truespeech);
if (truespeechIndex !== -1) languages.splice(truespeechIndex, 1);
}
if (this.comprehendLanguages && !this._isTruespeech(this.comprehendLanguages)) {
const comprehendLanguagesIndex = languages.findIndex((element) => element.attributes["data-language"] == this.comprehendLanguages);
const comprehendLanguagesIndex = languages.findIndex((element) => element.attributes["data-language"] === this.comprehendLanguages);
if (comprehendLanguagesIndex !== -1) languages.splice(comprehendLanguagesIndex, 1);
}
editorOptions.style_formats = [
...CONFIG.TinyMCE.style_formats,
{
title: "Polyglot",
items: languages,
},
];
editorOptions.formats = {
removeformat: [
// Default remove format configuration from tinyMCE
{
selector: "b,strong,em,i,font,u,strike,sub,sup,dfn,code,samp,kbd,var,cite,mark,q,del,ins",
remove: "all",
split: true,
expand: false,
block_expand: true,
deep: true,
},
{
selector: "span",
attributes: ["style", "class"],
remove: "empty",
split: true,
expand: false,
deep: true,
},
{
selector: "*",
attributes: ["style", "class"],
split: false,
expand: false,
deep: true,
},
// Add custom config to remove spans from polyglot when needed
{
selector: "span",
classes: "polyglot-journal",
attributes: ["title", "class", "data-language"],
remove: "all",
split: true,
expand: false,
deep: true,
},
],
};
return languages;
}

/* -------------------------------------------- */
Expand Down

0 comments on commit 459dae8

Please sign in to comment.