From 850d0762687ab719ad3c42b7e8997dcfc9db40b0 Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Thu, 26 Sep 2024 23:16:58 +0200 Subject: [PATCH] Using more modern ECMAScript --- .../LogarithmPlotter/js/history/common.mjs | 20 ++--- .../js/history/editproperty.mjs | 28 +++---- .../LogarithmPlotter/js/history/position.mjs | 25 +++--- .../LogarithmPlotter/js/module/interface.mjs | 83 ++++++++----------- .../ad5001/LogarithmPlotter/js/module/io.mjs | 2 +- .../LogarithmPlotter/js/module/latex.mjs | 20 +---- 6 files changed, 67 insertions(+), 111 deletions(-) diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/common.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/common.mjs index 41d28ce..9e59376 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/common.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/common.mjs @@ -85,20 +85,16 @@ export class Action { * @param {string} latexString - Source string of the latex. * @returns {Promise} */ - renderLatexAsHtml(latexString) { + async renderLatexAsHtml(latexString) { if(!Latex.enabled) throw new Error("Cannot render an item as LaTeX when LaTeX is disabled.") - return new Promise(resolve => { - let imgDepth = History.imageDepth - Latex.requestAsyncRender( - latexString, - imgDepth * (History.fontSize + 2), - History.themeTextColor - ).then((imgData) => { - const { source, width, height } = imgData - resolve(``) - }) - }) + const imgDepth = History.imageDepth + const { source, width, height } = await Latex.requestAsyncRender( + latexString, + imgDepth * (History.fontSize + 2), + History.themeTextColor + ) + return `` } /** diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/editproperty.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/editproperty.mjs index c4b9217..93cc680 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/editproperty.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/editproperty.mjs @@ -138,22 +138,16 @@ export default class EditedProperty extends Action { * * @return {Promise|string} */ - getHTMLString() { - return new Promise(resolve => { - const translation = qsTranslate("editproperty", '%1 of %2 changed from %3 to %4.') - .arg(this.targetPropertyReadable) - .arg(' ' + this.targetName + ' ') - // Check if we need to wait for LaTeX HTML to be rendered. - if(this.prevHTML !== undefined && this.nextHTML !== undefined) - resolve(translation.arg(this.prevHTML).arg(this.nextHTML)) - else - Promise.all(this._renderPromises).then((rendered) => { - // Rendered are (potentially) two HTML strings which are defined during rendering - this.prevHTML = this.prevHTML ?? rendered[0] - this.nextHTML = this.prevHTML ?? rendered[1] - resolve(translation.arg(this.prevHTML).arg(this.nextHTML)) - }) - }) - + async getHTMLString() { + const translation = qsTranslate("editproperty", '%1 of %2 changed from %3 to %4.') + .arg(this.targetPropertyReadable) + .arg(' ' + this.targetName + ' ') + // Check if we need to wait for LaTeX HTML to be rendered. + if(this.prevHTML === undefined || this.nextHTML === undefined) { + const [prevHTML, nextHTML] = await Promise.all(this._renderPromises) + this.prevHTML = this.prevHTML ?? prevHTML + this.nextHTML = this.nextHTML ?? nextHTML + } + return translation.arg(this.prevHTML).arg(this.nextHTML) } } diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/position.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/position.mjs index 1671036..b84c18b 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/position.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/history/position.mjs @@ -89,21 +89,16 @@ export default class EditedPosition extends Action { .arg(this.targetName).arg(this.prevString).arg(this.nextString) } - getHTMLString() { - return new Promise(resolve => { - const translation = qsTranslate("position", 'Position of %1 set from %2 to %3.') - .arg(' ' + this.targetName + ' ') - // Check if we need to wait for LaTeX HTML to be rendered. - if(this.prevHTML !== undefined && this.nextHTML !== undefined) - resolve(translation.arg(this.prevHTML).arg(this.nextHTML)) - else - Promise.all(this._renderPromises).then((rendered) => { - // Rendered are (potentially) two HTML strings which are defined during rendering - this.prevHTML = this.prevHTML ?? rendered[0] - this.nextHTML = this.nextHTML ?? rendered[1] - resolve(translation.arg(this.prevHTML).arg(this.nextHTML)) - }) - }) + async getHTMLString() { + const translation = qsTranslate("position", 'Position of %1 set from %2 to %3.') + .arg(' ' + this.targetName + ' ') + // Check if we need to wait for LaTeX HTML to be rendered. + if(this.prevHTML === undefined || this.nextHTML === undefined) { + const [prevHTML, nextHTML] = await Promise.all(this._renderPromises) + this.prevHTML = this.prevHTML ?? prevHTML + this.nextHTML = this.nextHTML ?? nextHTML + } + return translation.arg(this.prevHTML).arg(this.nextHTML) } } diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/module/interface.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/module/interface.mjs index 63ba88c..37f4f5b 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/module/interface.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/module/interface.mjs @@ -69,65 +69,50 @@ export class Interface { export class SettingsInterface extends Interface { - constructor() { - super() - this.width = NUMBER - this.height = NUMBER - this.xmin = NUMBER - this.ymax = NUMBER - this.xzoom = NUMBER - this.yzoom = NUMBER - this.xaxisstep = STRING - this.yaxisstep = STRING - this.xlabel = STRING - this.ylabel = STRING - this.linewidth = NUMBER - this.textsize = NUMBER - this.logscalex = BOOLEAN - this.showxgrad = BOOLEAN - this.showygrad = BOOLEAN - } + width = NUMBER + height = NUMBER + xmin = NUMBER + ymax = NUMBER + xzoom = NUMBER + yzoom = NUMBER + xaxisstep = STRING + yaxisstep = STRING + xlabel = STRING + ylabel = STRING + linewidth = NUMBER + textsize = NUMBER + logscalex = BOOLEAN + showxgrad = BOOLEAN + showygrad = BOOLEAN } export class CanvasInterface extends SettingsInterface { - constructor() { - super() - this.imageLoaders = OBJECT - /** @type {function(string): CanvasRenderingContext2D} */ - this.getContext = FUNCTION - /** @type {function(rect)} */ - this.markDirty = FUNCTION - /** @type {function(string)} */ - this.loadImage = FUNCTION - /** @type {function()} */ - this.requestPaint = FUNCTION - } + imageLoaders = OBJECT + /** @type {function(string): CanvasRenderingContext2D} */ + getContext = FUNCTION + /** @type {function(rect)} */ + markDirty = FUNCTION + /** @type {function(string)} */ + loadImage = FUNCTION + /** @type {function()} */ + requestPaint = FUNCTION } export class RootInterface extends Interface { - constructor() { - super() - this.width = NUMBER - this.height = NUMBER - this.updateObjectsLists = FUNCTION - } + width = NUMBER + height = NUMBER + updateObjectsLists = FUNCTION } export class DialogInterface extends Interface { - constructor() { - super() - this.show = FUNCTION - } + show = FUNCTION } export class HistoryInterface extends Interface { - constructor() { - super() - this.undo = FUNCTION - this.redo = FUNCTION - this.clear = FUNCTION - this.addToHistory = FUNCTION - this.unserialize = FUNCTION - this.serialize = FUNCTION - } + undo = FUNCTION + redo = FUNCTION + clear = FUNCTION + addToHistory = FUNCTION + unserialize = FUNCTION + serialize = FUNCTION } diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/module/io.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/module/io.mjs index 11ac25f..d4ff50c 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/module/io.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/module/io.mjs @@ -97,7 +97,7 @@ class IOAPI extends Module { * Loads the diagram from a certain \c filename. * @param {string} filename */ - loadDiagram(filename) { + async loadDiagram(filename) { if(!this.initialized) throw new Error("Attempting loadDiagram before initialize!") if(!History.initialized) throw new Error("Attempting loadDiagram before history is initialized!") let basename = filename.split("/").pop() diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/module/latex.mjs b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/module/latex.mjs index f2e9b0b..0071779 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/module/latex.mjs +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/js/module/latex.mjs @@ -65,19 +65,6 @@ class LatexAPI extends Module { this.enabled = Helper.getSettingBool("enable_latex") } - /** - * Prepares and renders a latex string into a png file. - * - * @param {string} markup - LaTeX markup to render. - * @param {number} fontSize - Font size (in pt) to render. - * @param {color} color - Color of the text to render. - * @returns {LatexRenderResult} - */ - renderSync(markup, fontSize, color) { - let args = Latex.render(markup, fontSize, color).split(",") - return new LatexRenderResult(...args) - } - /** * Checks if the given markup (with given font size and color) has already been * rendered, and if so, returns its data. Otherwise, returns null. @@ -103,10 +90,9 @@ class LatexAPI extends Module { * @param {color} color - Color of the text to render. * @returns {Promise} */ - requestAsyncRender(markup, fontSize, color) { - return new Promise(resolve => { - resolve(this.renderSync(markup, fontSize, color)) - }) + async requestAsyncRender(markup, fontSize, color) { + let args = Latex.render(markup, fontSize, color).split(",") + return new LatexRenderResult(...args) } /**