diff --git a/src/core/Chef.mjs b/src/core/Chef.mjs index b9eeba03..0fcee7f5 100755 --- a/src/core/Chef.mjs +++ b/src/core/Chef.mjs @@ -168,6 +168,18 @@ class Chef { return await newDish.get(type); } + /** + * Gets the title of a dish and returns it + * + * @param {Dish} dish + * @param {number} [maxLength=100] + * @returns {string} + */ + async getDishTitle(dish, maxLength=100) { + const newDish = new Dish(dish); + return await newDish.getTitle(maxLength); + } + } export default Chef; diff --git a/src/core/ChefWorker.js b/src/core/ChefWorker.js index 078842a1..14084f56 100644 --- a/src/core/ChefWorker.js +++ b/src/core/ChefWorker.js @@ -68,6 +68,9 @@ self.addEventListener("message", function(e) { case "getDishAs": getDishAs(r.data); break; + case "getDishTitle": + getDishTitle(r.data); + break; case "docURL": // Used to set the URL of the current document so that scripts can be // imported into an inline worker. @@ -158,6 +161,26 @@ async function getDishAs(data) { } +/** + * Gets the dish title + * + * @param {object} data + * @param {Dish} data.dish + * @param {number} data.maxLength + * @param {number} data.id + */ +async function getDishTitle(data) { + const title = await self.chef.getDishTitle(data.dish, data.maxLength); + self.postMessage({ + action: "dishReturned", + data: { + value: title, + id: data.id + } + }); +} + + /** * Calculates highlight offsets if possible. * diff --git a/src/core/Dish.mjs b/src/core/Dish.mjs index 96aea716..7d1ec7b5 100755 --- a/src/core/Dish.mjs +++ b/src/core/Dish.mjs @@ -141,6 +141,32 @@ class Dish { } + /** + * Returns the title of the data up to the specified length + * + * @param {number} maxLength - The maximum title length + * @returns {string} + */ + async getTitle(maxLength) { + let title = ""; + + // LIST OF FILES - Say e.g. "3 files" + switch (this.type) { + case Dish.FILE: + title = this.value.name; + break; + case Dish.LIST_FILE: + title = `${this.value.length} file(s)`; + break; + default: + title = await this.get("string"); + } + + title = title.slice(0, maxLength); + return title; + } + + /** * Translates the data to the given type format. * diff --git a/src/web/waiters/OutputWaiter.mjs b/src/web/waiters/OutputWaiter.mjs index 39b41cf1..04279b2f 100755 --- a/src/web/waiters/OutputWaiter.mjs +++ b/src/web/waiters/OutputWaiter.mjs @@ -442,7 +442,6 @@ class OutputWaiter { }); } - /** * Retrieves the dish as an ArrayBuffer, returning the cached version if possible. * @@ -457,6 +456,21 @@ class OutputWaiter { }); } + /** + * Retrieves the title of the Dish as a string + * + * @param {Dish} dish + * @param {number} maxLength + * @returns {string} + */ + async getDishTitle(dish, maxLength) { + return await new Promise(resolve => { + this.manager.worker.getDishTitle(dish, maxLength, r=> { + resolve(r.value); + }); + }); + } + /** * Save bombe object then remove it from the DOM so that it does not cause performance issues. */ @@ -970,10 +984,9 @@ class OutputWaiter { let tabStr = ""; if (dish !== null) { - tabStr = await this.getDishStr(this.getOutputDish(inputNum)); + tabStr = await this.getDishTitle(this.getOutputDish(inputNum), 100); tabStr = tabStr.replace(/[\n\r]/g, ""); } - tabStr = tabStr.slice(0, 200); this.manager.tabs.updateOutputTabHeader(inputNum, tabStr); if (this.manager.worker.recipeConfig !== undefined) { this.manager.tabs.updateOutputTabProgress(inputNum, this.outputs[inputNum].progress, this.manager.worker.recipeConfig.length); diff --git a/src/web/waiters/WorkerWaiter.mjs b/src/web/waiters/WorkerWaiter.mjs index 89cd2646..1099cab8 100644 --- a/src/web/waiters/WorkerWaiter.mjs +++ b/src/web/waiters/WorkerWaiter.mjs @@ -631,6 +631,31 @@ class WorkerWaiter { }); } + /** + * Asks the ChefWorker to get the title of the dish + * + * @param {Dish} dish + * @param {number} maxLength + * @param {Function} callback + * @returns {string} + */ + getDishTitle(dish, maxLength, callback) { + const id = this.callbackID++; + + this.callbacks[id] = callback; + + if (this.dishWorker === null) this.setupDishWorker(); + + this.dishWorker.postMessage({ + action: "getDishTitle", + data: { + dish: dish, + maxLength: maxLength, + id: id + } + }); + } + /** * Sets the console log level in the workers. */