UNPKG

@launchmenu/core

Version:

An environment for visual keyboard controlled applets

65 lines 6.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getEditTargetTextField = exports.performNormalizedTextEdit = void 0; const CompoundTextEditCommand_1 = require("./commands/CompoundTextEditCommand"); const TextAlterationTools_1 = require("./commands/TextAlterationTools"); const TextEditCommand_1 = require("./commands/TextEditCommand"); /** * Performs a text edit using a normalized callback, either passing a command or performing the alteration immediately * @param target The target of the text change * @param edit The callback to perform the text change */ function performNormalizedTextEdit(target, edit) { const textField = "textField" in target ? target.textField : target; // Use the callback to retrieve the changes const result = edit(textField, "textField" in target); // Retrieve the potential result command, or result alterations const resultCommand = "getAlterations" in result ? result : undefined; const resultAlterations = "alterations" in result ? { ...result, alternations: result.alterations.sort(({ start: a }, { start: b }) => a - b), } : undefined; if (resultAlterations === null || resultAlterations === void 0 ? void 0 : resultAlterations.alterations.some((item, i, all) => { var _a; return item.end > ((_a = all[i + 1]) === null || _a === void 0 ? void 0 : _a.start); })) throw Error("Alterations may not overlap each other, use a compound command instead"); // Check the result and perform the necessary action if ("textField" in target) { // If a command was returned, simply return the command if (resultCommand) target.onChange(resultCommand); // If alterations were returned, turn it into a command else if (resultAlterations) { const { alterations, selection } = resultAlterations; const commands = alterations.map(alteration => new TextEditCommand_1.TextEditCommand(textField, alteration, selection)); if (commands.length == 1) target.onChange(commands[0]); else if (commands.length > 1) { const command = new CompoundTextEditCommand_1.CompoundTextEditCommand(commands); target.onChange(command); } } } else { // If a command was returned, simply execute the command if (resultCommand) resultCommand.execute(); // If alterations were returned, combine them into the new text else if (resultAlterations) { const { alterations, selection } = resultAlterations; const newText = TextAlterationTools_1.TextAlterationTools.performAlterations(textField.get(), alterations); target.set(newText); if (selection) target.setSelection(selection); } } } exports.performNormalizedTextEdit = performNormalizedTextEdit; /** * Retrieves the text field given a text edit target * @param targetField The text edit target * @returns The text field */ const getEditTargetTextField = (targetField) => "textField" in targetField ? targetField.textField : targetField; exports.getEditTarget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