Proyectos de Subversion Moodle

Rev

Autoría | Ultima modificación | Ver Log |

{"version":3,"file":"plugin.min.js","sources":["../src/plugin.js"],"sourcesContent":["// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.\n\n/**\n * Tiny tiny_html for Moodle.\n *\n * @module      tiny_html/plugin\n * @copyright   2023 Matt Porritt <matt.porritt@moodle.com>\n * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport {getTinyMCE} from 'editor_tiny/loader';\nimport {getPluginMetadata} from 'editor_tiny/utils';\n\nimport {component, pluginName, codeMirrorStyle} from './common';\n\n/* eslint-disable camelcase */\nimport {html_beautify} from './beautify/beautify-html';\nimport {get_strings} from 'core/str';\n/* eslint-enable camelcase */\nimport {\n    EditorState,\n    EditorView,\n    basicSetup,\n    lang,\n} from './codemirror-lazy';\n\n/**\n * Options for the html_beautify function.\n * We disable the camelCase check here as these are\n * variables that we are passing to the js-beautify library.\n */\n/* eslint-disable camelcase */\nconst beautifyOptions = {\n    indent_size: 2,\n    wrap_line_length: 80,\n    unformatted: [],\n};\n/* eslint-enable camelcase */\n\n// Set up the tiny_html Plugin.\n// eslint-disable-next-line no-async-promise-executor\nexport default new Promise(async(resolve) => {\n    // Note: The PluginManager.add function does not support asynchronous configuration.\n    // Perform any asynchronous configuration here, and then call the PluginManager.add function.\n    const [\n        tinyMCE,\n        pluginMetadata,\n        buttonStrings,\n    ] = await Promise.all([\n        getTinyMCE(),\n        getPluginMetadata(component, pluginName),\n        get_strings([\n            {key: 'cancel', component: 'moodle'},\n            {key: 'save', component: 'moodle'},\n        ])\n    ]);\n\n    // Reminder: Any asynchronous code must be run before this point.\n    tinyMCE.PluginManager.add(pluginName, (editor) => {\n        // Initial configuration for TinyMCE editor the windowManager.\n        const windowManagerConfig = {\n            title: 'Source code',\n            size: 'large',\n            body: {\n                type: 'panel',\n                items: [\n                    {\n                        type: 'htmlpanel',\n                        html: '<div id=\"' + editor.id + '_codeMirrorContainer\" style=\"height: 100%;\"></div>',\n                    },\n                ],\n            },\n            buttons: null,\n            initialData: null,\n            onSubmit: null,\n        };\n\n        // Overriding the default 'mceCodeEditor' command\n        editor.addCommand('mceCodeEditor', () => {\n            // Get the current content of the editor\n            // eslint-disable-next-line camelcase\n            const content = editor.getContent({source_view: true});\n\n            // Beautify the content using html_beautify\n            const beautifiedContent = html_beautify(content, beautifyOptions);\n\n            // Create the CodeMirror instance\n            let cmInstance;\n\n            let state = EditorState.create({\n                doc: beautifiedContent,\n                // This is where basicSetup should go as [basicSetup, ...].\n                extensions: [\n                    basicSetup,\n                    EditorState.tabSize.of(2),\n                    // Bring in all language extensions.\n                    ...Object.entries(lang).map(([, languagePlugin]) => languagePlugin()),\n                ],\n            });\n\n            // Create a new window to display the beautified code\n            editor.windowManager.open({\n                ...windowManagerConfig,\n                onSubmit: (api) => {\n                    const cmContent = cmInstance.state.doc.toString();\n                    // eslint-disable-next-line camelcase\n                    editor.setContent(cmContent, {source_view: true});\n                    api.close();\n                },\n                buttons: [\n                    {\n                        type: 'cancel',\n                        text: buttonStrings[0],\n                    },\n                    {\n                        type: 'submit',\n                        text: buttonStrings[1],\n                        primary: true,\n                    },\n                ]\n            });\n\n            const container = document.getElementById(editor.id + '_codeMirrorContainer');\n            // Create a shadow root for the CodeMirror instance.\n            // This is required to prevent the TinyMCE editor styles from overriding the CodeMirror ones.\n            const shadowRoot = container.attachShadow({mode: \"open\"});\n\n            // Add the styles to the shadow root\n            const style = document.createElement('style');\n            style.textContent = codeMirrorStyle;\n            shadowRoot.appendChild(style);\n\n            // Create a new div and add the class 'my-codemirror-container'\n            const div = document.createElement('div');\n            div.classList.add('modal-codemirror-container');\n            shadowRoot.appendChild(div);\n\n            // Create the CodeMirror instance\n            cmInstance = new EditorView({\n                state,\n                parent: div,\n            });\n\n            // Add an event listener to the shadow root to listen for the tab key press.\n            shadowRoot.addEventListener('keydown', (event) => {\n                // If the tab key is pressed, prevent the default action and select the save button.\n                // We need to do this as the shadow root is not part of the DOM, so the tab key will not\n                // be caught by the TinyMCE dialog.\n                if (event.key === 'Tab') {\n                    event.preventDefault();\n                    const codeMirrorContainer = document.getElementById(editor.id + '_codeMirrorContainer');\n                    const dialogElement = codeMirrorContainer.closest('.tox-dialog');\n                    const cancelButton = dialogElement.querySelector('button[title=\"' + buttonStrings[1] + '\"]');\n                    cancelButton.focus();\n                }\n            });\n\n        });\n        // Return the pluginMetadata object. This is used by TinyMCE to display a help link for your plugin.\n        return pluginMetadata;\n    });\n\n    resolve(pluginName);\n});\n"],"names":["beautifyOptions","indent_size","wrap_line_length","unformatted","Promise","async","tinyMCE","pluginMetadata","buttonStrings","all","component","pluginName","key","PluginManager","add","editor","windowManagerConfig","title","size","body","type","items","html","id","buttons","initialData","onSubmit","addCommand","content","getContent","source_view","beautifiedContent","cmInstance","state","EditorState","create","doc","extensions","basicSetup","tabSize","of","Object","entries","lang","map","_ref","languagePlugin","windowManager","open","api","cmContent","toString","setContent","close","text","primary","shadowRoot","document","getElementById","attachShadow","mode","style","createElement","textContent","codeMirrorStyle","appendChild","div","classList","EditorView","parent","addEventListener","event","preventDefault","closest","querySelector","focus","resolve"],"mappings":";;;;;;;;MA6CMA,gBAAkB,CACpBC,YAAa,EACbC,iBAAkB,GAClBC,YAAa,iBAMF,IAAIC,SAAQC,MAAAA,gBAInBC,QACAC,eACAC,qBACMJ,QAAQK,IAAI,EAClB,yBACA,4BAAkBC,kBAAWC,qBAC7B,oBAAY,CACR,CAACC,IAAK,SAAUF,UAAW,UAC3B,CAACE,IAAK,OAAQF,U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