AutorÃa | Ultima modificación | Ver Log |
{"version":3,"file":"equation.min.js","sources":["../src/equation.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Equation helper for Tiny Equation plugin.\n *\n * @module tiny_equation/equation\n * @copyright 2022 Huong Nguyen <huongnv13@gmail.com>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\ni
mport Selectors from 'tiny_equation/selectors';\n\nlet sourceEquation = null;\n\n/**\n * Get the source equation.\n * @returns {Object}\n */\nexport const getSourceEquation = () => sourceEquation;\n\n/**\n * Get selected equation.\n * @param {TinyMCE} editor\n * @returns {boolean}\n */\nexport const getSelectedEquation = (editor) => {\n const currentSelection = editor.selection.getSel();\n if (!currentSelection) {\n // Do the early return if there is no text selected.\n return false;\n }\n const textSelection = editor.selection.getNode().textContent;\n const currentCaretPos = currentSelection.focusOffset;\n let returnValue = false;\n\n Selectors.equationPatterns.forEach((pattern) => {\n // For each pattern in turn, find all whole matches (including the delimiters).\n const regexPattern = new RegExp(pattern.source, \"g\");\n [...textSelection.matchAll(regexPattern)].forEach((matches) => {\n const match = matches[0];\n // Check each
occurrence of this match.\n let startIndex = 0;\n const startOuter = textSelection.indexOf(match, startIndex);\n const endOuter = startOuter + match.length;\n\n // This match is in our current position - fetch the innerMatch data.\n const innerMatch = match.match(pattern);\n if (innerMatch && innerMatch.length) {\n // We need the start and end of the inner match for later.\n const startInner = textSelection.indexOf(innerMatch[1], startOuter);\n const endInner = startInner + innerMatch[1].length;\n\n // We need to check the caret position before returning the match.\n if (currentCaretPos >= startOuter && currentCaretPos <= endOuter) {\n // We'll be returning the inner match for use in the editor itself.\n returnValue = innerMatch[1];\n\n // Save all data for later.\n sourceEquation = {\n
// Inner match data.\n startInnerPosition: startInner,\n endInnerPosition: endInner,\n innerMatch: innerMatch\n };\n\n return;\n }\n }\n\n // Update the startIndex to match the end of the current match so that we can continue hunting\n // for further matches.\n startIndex = endOuter;\n });\n });\n\n // We trim the equation when we load it and then add spaces when we save it.\n if (returnValue !== false) {\n returnValue = returnValue.trim();\n } else {\n // Clear the saved source equation.\n sourceEquation = null;\n }\n\n return returnValue;\n};\n\n/**\n * Get current equation data.\n * @param {TinyMCE} editor\n * @returns {{}}\n */\nexport const getCurrentEquationData = (editor) => {\n let properties = {};\n const equation = getSelectedEquation(editor);\n if (equation) {\n
properties.equation = equation;\n }\n\n return properties;\n};\n\n/**\n * Handle insertion of a new equation, or update of an existing one.\n * @param {Element} currentForm\n * @param {TinyMCE} editor\n */\nexport const setEquation = (currentForm, editor) => {\n const input = currentForm.querySelector(Selectors.elements.equationTextArea);\n const sourceEquation = getSourceEquation();\n let value = input.value;\n\n if (value !== '') {\n if (sourceEquation) {\n const selectedNode = editor.selection.getNode();\n const text = selectedNode.textContent;\n value = ' ' + value + ' ';\n selectedNode.textContent = text.slice(0, sourceEquation.startInnerPosition)\n + value\n + text.slice(sourceEquation.endInnerPosition);\n } else {\n value = Selectors.delimiters.start + ' ' + value + ' ' + Selectors.delimiters.end;\n editor.insertContent(value);\n }\n }\n};\n"],"names":["sourceEq
uation","getSourceEquation","getSelectedEquation","editor","currentSelection","selection","getSel","textSelection","getNode","textContent","currentCaretPos","focusOffset","returnValue","equationPatterns","forEach","pattern","regexPattern","RegExp","source","matchAll","matches","match","startIndex","startOuter","indexOf","endOuter","length","innerMatch","startInner","endInner","startInnerPosition","endInnerPosition","trim","properties","equation","currentForm","input","querySelector","Selectors","elements","equationTextArea","value","selectedNode","text","slice","delimiters","start","end","insertContent"],"m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