Using more modern ECMAScript

This commit is contained in:
Adsooi 2024-09-26 23:16:58 +02:00
parent 95b47effdf
commit 850d076268
Signed by: Ad5001
GPG key ID: EF45F9C6AFE20160
6 changed files with 67 additions and 111 deletions

View file

@ -85,20 +85,16 @@ export class Action {
* @param {string} latexString - Source string of the latex. * @param {string} latexString - Source string of the latex.
* @returns {Promise<string>} * @returns {Promise<string>}
*/ */
renderLatexAsHtml(latexString) { async renderLatexAsHtml(latexString) {
if(!Latex.enabled) if(!Latex.enabled)
throw new Error("Cannot render an item as LaTeX when LaTeX is disabled.") throw new Error("Cannot render an item as LaTeX when LaTeX is disabled.")
return new Promise(resolve => { const imgDepth = History.imageDepth
let imgDepth = History.imageDepth const { source, width, height } = await Latex.requestAsyncRender(
Latex.requestAsyncRender( latexString,
latexString, imgDepth * (History.fontSize + 2),
imgDepth * (History.fontSize + 2), History.themeTextColor
History.themeTextColor )
).then((imgData) => { return `<img src="${source}" width="${width/imgDepth}" height="${height/imgDepth}" style="vertical-align: middle"/>`
const { source, width, height } = imgData
resolve(`<img src="${source}" width="${width/imgDepth}" height="${height/imgDepth}" style="vertical-align: middle"/>`)
})
})
} }
/** /**

View file

@ -138,22 +138,16 @@ export default class EditedProperty extends Action {
* *
* @return {Promise<string>|string} * @return {Promise<string>|string}
*/ */
getHTMLString() { async getHTMLString() {
return new Promise(resolve => { const translation = qsTranslate("editproperty", '%1 of %2 changed from %3 to %4.')
const translation = qsTranslate("editproperty", '%1 of %2 changed from %3 to %4.') .arg(this.targetPropertyReadable)
.arg(this.targetPropertyReadable) .arg('<b style="font-size: 15px;">&nbsp;' + this.targetName + '&nbsp;</b>')
.arg('<b style="font-size: 15px;">&nbsp;' + this.targetName + '&nbsp;</b>') // Check if we need to wait for LaTeX HTML to be rendered.
// Check if we need to wait for LaTeX HTML to be rendered. if(this.prevHTML === undefined || this.nextHTML === undefined) {
if(this.prevHTML !== undefined && this.nextHTML !== undefined) const [prevHTML, nextHTML] = await Promise.all(this._renderPromises)
resolve(translation.arg(this.prevHTML).arg(this.nextHTML)) this.prevHTML = this.prevHTML ?? prevHTML
else this.nextHTML = this.nextHTML ?? nextHTML
Promise.all(this._renderPromises).then((rendered) => { }
// Rendered are (potentially) two HTML strings which are defined during rendering return translation.arg(this.prevHTML).arg(this.nextHTML)
this.prevHTML = this.prevHTML ?? rendered[0]
this.nextHTML = this.prevHTML ?? rendered[1]
resolve(translation.arg(this.prevHTML).arg(this.nextHTML))
})
})
} }
} }

View file

@ -89,21 +89,16 @@ export default class EditedPosition extends Action {
.arg(this.targetName).arg(this.prevString).arg(this.nextString) .arg(this.targetName).arg(this.prevString).arg(this.nextString)
} }
getHTMLString() { async getHTMLString() {
return new Promise(resolve => { const translation = qsTranslate("position", 'Position of %1 set from %2 to %3.')
const translation = qsTranslate("position", 'Position of %1 set from %2 to %3.') .arg('<b style="font-size: 15px;">&nbsp;' + this.targetName + '&nbsp;</b>')
.arg('<b style="font-size: 15px;">&nbsp;' + this.targetName + '&nbsp;</b>') // Check if we need to wait for LaTeX HTML to be rendered.
// Check if we need to wait for LaTeX HTML to be rendered. if(this.prevHTML === undefined || this.nextHTML === undefined) {
if(this.prevHTML !== undefined && this.nextHTML !== undefined) const [prevHTML, nextHTML] = await Promise.all(this._renderPromises)
resolve(translation.arg(this.prevHTML).arg(this.nextHTML)) this.prevHTML = this.prevHTML ?? prevHTML
else this.nextHTML = this.nextHTML ?? nextHTML
Promise.all(this._renderPromises).then((rendered) => { }
// Rendered are (potentially) two HTML strings which are defined during rendering return translation.arg(this.prevHTML).arg(this.nextHTML)
this.prevHTML = this.prevHTML ?? rendered[0]
this.nextHTML = this.nextHTML ?? rendered[1]
resolve(translation.arg(this.prevHTML).arg(this.nextHTML))
})
})
} }
} }

View file

@ -69,65 +69,50 @@ export class Interface {
export class SettingsInterface extends Interface { export class SettingsInterface extends Interface {
constructor() { width = NUMBER
super() height = NUMBER
this.width = NUMBER xmin = NUMBER
this.height = NUMBER ymax = NUMBER
this.xmin = NUMBER xzoom = NUMBER
this.ymax = NUMBER yzoom = NUMBER
this.xzoom = NUMBER xaxisstep = STRING
this.yzoom = NUMBER yaxisstep = STRING
this.xaxisstep = STRING xlabel = STRING
this.yaxisstep = STRING ylabel = STRING
this.xlabel = STRING linewidth = NUMBER
this.ylabel = STRING textsize = NUMBER
this.linewidth = NUMBER logscalex = BOOLEAN
this.textsize = NUMBER showxgrad = BOOLEAN
this.logscalex = BOOLEAN showygrad = BOOLEAN
this.showxgrad = BOOLEAN
this.showygrad = BOOLEAN
}
} }
export class CanvasInterface extends SettingsInterface { export class CanvasInterface extends SettingsInterface {
constructor() { imageLoaders = OBJECT
super() /** @type {function(string): CanvasRenderingContext2D} */
this.imageLoaders = OBJECT getContext = FUNCTION
/** @type {function(string): CanvasRenderingContext2D} */ /** @type {function(rect)} */
this.getContext = FUNCTION markDirty = FUNCTION
/** @type {function(rect)} */ /** @type {function(string)} */
this.markDirty = FUNCTION loadImage = FUNCTION
/** @type {function(string)} */ /** @type {function()} */
this.loadImage = FUNCTION requestPaint = FUNCTION
/** @type {function()} */
this.requestPaint = FUNCTION
}
} }
export class RootInterface extends Interface { export class RootInterface extends Interface {
constructor() { width = NUMBER
super() height = NUMBER
this.width = NUMBER updateObjectsLists = FUNCTION
this.height = NUMBER
this.updateObjectsLists = FUNCTION
}
} }
export class DialogInterface extends Interface { export class DialogInterface extends Interface {
constructor() { show = FUNCTION
super()
this.show = FUNCTION
}
} }
export class HistoryInterface extends Interface { export class HistoryInterface extends Interface {
constructor() { undo = FUNCTION
super() redo = FUNCTION
this.undo = FUNCTION clear = FUNCTION
this.redo = FUNCTION addToHistory = FUNCTION
this.clear = FUNCTION unserialize = FUNCTION
this.addToHistory = FUNCTION serialize = FUNCTION
this.unserialize = FUNCTION
this.serialize = FUNCTION
}
} }

View file

@ -97,7 +97,7 @@ class IOAPI extends Module {
* Loads the diagram from a certain \c filename. * Loads the diagram from a certain \c filename.
* @param {string} filename * @param {string} filename
*/ */
loadDiagram(filename) { async loadDiagram(filename) {
if(!this.initialized) throw new Error("Attempting loadDiagram before initialize!") if(!this.initialized) throw new Error("Attempting loadDiagram before initialize!")
if(!History.initialized) throw new Error("Attempting loadDiagram before history is initialized!") if(!History.initialized) throw new Error("Attempting loadDiagram before history is initialized!")
let basename = filename.split("/").pop() let basename = filename.split("/").pop()

View file

@ -65,19 +65,6 @@ class LatexAPI extends Module {
this.enabled = Helper.getSettingBool("enable_latex") 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 * Checks if the given markup (with given font size and color) has already been
* rendered, and if so, returns its data. Otherwise, returns null. * 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. * @param {color} color - Color of the text to render.
* @returns {Promise<LatexRenderResult>} * @returns {Promise<LatexRenderResult>}
*/ */
requestAsyncRender(markup, fontSize, color) { async requestAsyncRender(markup, fontSize, color) {
return new Promise(resolve => { let args = Latex.render(markup, fontSize, color).split(",")
resolve(this.renderSync(markup, fontSize, color)) return new LatexRenderResult(...args)
})
} }
/** /**