Using more modern ECMAScript

This commit is contained in:
Ad5001 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.
* @returns {Promise<string>}
*/
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(`<img src="${source}" width="${width/imgDepth}" height="${height/imgDepth}" style="vertical-align: middle"/>`)
})
})
const imgDepth = History.imageDepth
const { source, width, height } = await Latex.requestAsyncRender(
latexString,
imgDepth * (History.fontSize + 2),
History.themeTextColor
)
return `<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}
*/
getHTMLString() {
return new Promise(resolve => {
const translation = qsTranslate("editproperty", '%1 of %2 changed from %3 to %4.')
.arg(this.targetPropertyReadable)
.arg('<b style="font-size: 15px;">&nbsp;' + this.targetName + '&nbsp;</b>')
// 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('<b style="font-size: 15px;">&nbsp;' + this.targetName + '&nbsp;</b>')
// 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)
}
}

View file

@ -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('<b style="font-size: 15px;">&nbsp;' + this.targetName + '&nbsp;</b>')
// 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('<b style="font-size: 15px;">&nbsp;' + this.targetName + '&nbsp;</b>')
// 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)
}
}

View file

@ -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
}

View file

@ -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()

View file

@ -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<LatexRenderResult>}
*/
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)
}
/**