Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// This file is part of Moodle - http://moodle.org/
2
//
3
// Moodle is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// Moodle is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
 
16
/**
17
 * Link helper for Tiny Link plugin.
18
 *
19
 * @module      tiny_link/link
20
 * @copyright   2023 Huong Nguyen <huongnv13@gmail.com>
21
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
import Templates from 'core/templates';
25
import Pending from 'core/pending';
26
import Selectors from 'tiny_link/selectors';
27
 
28
/**
29
 * Handle insertion of a new link, or update of an existing one.
30
 *
31
 * @param {Element} currentForm
32
 * @param {TinyMCE} editor
33
 */
34
export const setLink = (currentForm, editor) => {
35
    const input = currentForm.querySelector(Selectors.elements.urlEntry);
36
    let value = input.value;
37
 
38
    if (value !== '') {
39
        const pendingPromise = new Pending('tiny_link/setLink');
40
        // We add a prefix if it is not already prefixed.
41
        value = value.trim();
42
        const expr = new RegExp(/^[a-zA-Z]*\.*\/|^#|^[a-zA-Z]*:/);
43
        if (!expr.test(value)) {
44
            value = 'http://' + value;
45
        }
46
 
47
        // Add the link.
48
        setLinkOnSelection(currentForm, editor, value).then(pendingPromise.resolve);
49
    }
50
};
51
 
52
/**
53
 * Handle unlink of a link
54
 *
55
 * @param {TinyMCE} editor
56
 */
57
export const unSetLink = (editor) => {
58
    if (editor.hasPlugin('rtc', true)) {
59
        editor.execCommand('unlink');
60
    } else {
61
        const dom = editor.dom;
62
        const selection = editor.selection;
63
        const bookmark = selection.getBookmark();
64
        const rng = selection.getRng().cloneRange();
65
        const startAnchorElm = dom.getParent(rng.startContainer, 'a[href]', editor.getBody());
66
        const endAnchorElm = dom.getParent(rng.endContainer, 'a[href]', editor.getBody());
67
        if (startAnchorElm) {
68
            rng.setStartBefore(startAnchorElm);
69
        }
70
        if (endAnchorElm) {
71
            rng.setEndAfter(endAnchorElm);
72
        }
73
        selection.setRng(rng);
74
        editor.execCommand('unlink');
75
        selection.moveToBookmark(bookmark);
76
    }
77
};
78
 
79
/**
80
 * Final step setting the anchor on the selection.
81
 *
82
 * @param {Element} currentForm
83
 * @param {TinyMCE} editor
84
 * @param {String} url URL the link will point to.
85
 */
86
const setLinkOnSelection = async(currentForm, editor, url) => {
87
    const urlText = currentForm.querySelector(Selectors.elements.urlText);
88
    const target = currentForm.querySelector(Selectors.elements.openInNewWindow);
1441 ariadna 89
    const selectedNode = editor.selection.getNode();
90
    const isImage = selectedNode && selectedNode.nodeName.toLowerCase() === 'img';
1 efrain 91
    let textToDisplay = urlText.value.replace(/(<([^>]+)>)/gi, "").trim();
92
 
93
    if (textToDisplay === '') {
94
        textToDisplay = url;
95
    }
96
 
97
    const context = {
98
        url: url,
99
        newwindow: target.checked,
100
    };
1441 ariadna 101
    if (urlText.getAttribute('data-link-on-element') || isImage) {
1 efrain 102
        context.title = textToDisplay;
1441 ariadna 103
        context.name = selectedNode.outerHTML;
1 efrain 104
    } else {
105
        context.name = textToDisplay;
106
    }
107
    const {html} = await Templates.renderForPromise('tiny_link/embed_link', context);
108
    const currentLink = getSelectedLink(editor);
109
    if (currentLink) {
110
        currentLink.outerHTML = html;
111
    } else {
112
        editor.insertContent(html);
113
    }
114
};
115
 
116
/**
117
 * Get current link data.
118
 *
119
 * @param {TinyMCE} editor
120
 * @returns {{}}
121
 */
122
export const getCurrentLinkData = (editor) => {
123
    let properties = {};
124
    const link = getSelectedLink(editor);
125
    if (link) {
126
        const url = link.getAttribute('href');
127
        const target = link.getAttribute('target');
128
        const textToDisplay = link.innerText;
129
        const title = link.getAttribute('title');
130
 
131
        if (url !== '') {
132
            properties.url = url;
133
        }
134
        if (target === '_blank') {
135
            properties.newwindow = true;
136
        }
137
        if (title && title !== '') {
138
            properties.urltext = title.trim();
139
        } else if (textToDisplay !== '') {
140
            properties.urltext = textToDisplay.trim();
141
        }
142
    } else {
143
        // Check if the user is selecting some text before clicking on the Link button.
144
        const selectedNode = editor.selection.getNode();
145
        if (selectedNode) {
146
            const textToDisplay = getTextSelection(editor);
147
            if (textToDisplay !== '') {
148
                properties.urltext = textToDisplay.trim();
149
                properties.hasTextToDisplay = true;
150
                properties.hasPlainTextSelected = true;
151
            } else {
152
                if (selectedNode.getAttribute('data-mce-selected')) {
153
                    properties.setLinkOnElement = true;
154
                }
155
            }
156
        }
157
    }
158
 
159
    return properties;
160
};
161
 
162
/**
163
 * Get selected link.
164
 *
165
 * @param {TinyMCE} editor
166
 * @returns {Element}
167
 */
168
const getSelectedLink = (editor) => {
169
    return getAnchorElement(editor);
170
};
171
 
172
/**
173
 * Get anchor element.
174
 *
175
 * @param {TinyMCE} editor
176
 * @param {Element} selectedElm
177
 * @returns {Element}
178
 */
179
const getAnchorElement = (editor, selectedElm) => {
180
    selectedElm = selectedElm || editor.selection.getNode();
181
    return editor.dom.getParent(selectedElm, 'a[href]');
182
};
183
 
184
/**
185
 * Get only the selected text.
186
 * In some cases, window.getSelection() is not run as expected. We should only get the text value
187
 * For ex: <img src="" alt="XYZ">Some text here
188
 *          window.getSelection() will return XYZSome text here
189
 *
190
 * @param {TinyMCE} editor
191
 * @return {string} Selected text
192
 */
193
const getTextSelection = (editor) => {
194
    let selText = '';
195
    const sel = editor.selection.getSel();
196
    const rangeCount = sel.rangeCount;
197
    if (rangeCount) {
198
        let rangeTexts = [];
199
        for (let i = 0; i < rangeCount; ++i) {
200
            rangeTexts.push('' + sel.getRangeAt(i));
201
        }
202
        selText = rangeTexts.join('');
203
    }
204
 
205
    return selText;
206
};
207
 
208
/**
209
 * Check the current selected element is an anchor or not.
210
 *
211
 * @param {TinyMCE} editor
212
 * @param {Element} selectedElm
213
 * @returns {boolean}
214
 */
215
const isInAnchor = (editor, selectedElm) => getAnchorElement(editor, selectedElm) !== null;
216
 
217
/**
218
 * Change state of button.
219
 *
220
 * @param {TinyMCE} editor
221
 * @param {function()} toggler
222
 * @returns {function()}
223
 */
224
const toggleState = (editor, toggler) => {
225
    editor.on('NodeChange', toggler);
226
    return () => editor.off('NodeChange', toggler);
227
};
228
 
229
/**
230
 * Change the active state of button.
231
 *
232
 * @param {TinyMCE} editor
233
 * @returns {function(*): function(): *}
234
 */
235
export const toggleActiveState = (editor) => (api) => {
236
    const updateState = () => api.setActive(!editor.mode.isReadOnly() && isInAnchor(editor, editor.selection.getNode()));
237
    updateState();
238
    return toggleState(editor, updateState);
239
};