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);
|
|
|
89 |
let textToDisplay = urlText.value.replace(/(<([^>]+)>)/gi, "").trim();
|
|
|
90 |
|
|
|
91 |
if (textToDisplay === '') {
|
|
|
92 |
textToDisplay = url;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
const context = {
|
|
|
96 |
url: url,
|
|
|
97 |
newwindow: target.checked,
|
|
|
98 |
};
|
|
|
99 |
if (urlText.getAttribute('data-link-on-element')) {
|
|
|
100 |
context.title = textToDisplay;
|
|
|
101 |
context.name = editor.selection.getNode().outerHTML;
|
|
|
102 |
} else {
|
|
|
103 |
context.name = textToDisplay;
|
|
|
104 |
}
|
|
|
105 |
const {html} = await Templates.renderForPromise('tiny_link/embed_link', context);
|
|
|
106 |
const currentLink = getSelectedLink(editor);
|
|
|
107 |
if (currentLink) {
|
|
|
108 |
currentLink.outerHTML = html;
|
|
|
109 |
} else {
|
|
|
110 |
editor.insertContent(html);
|
|
|
111 |
}
|
|
|
112 |
};
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* Get current link data.
|
|
|
116 |
*
|
|
|
117 |
* @param {TinyMCE} editor
|
|
|
118 |
* @returns {{}}
|
|
|
119 |
*/
|
|
|
120 |
export const getCurrentLinkData = (editor) => {
|
|
|
121 |
let properties = {};
|
|
|
122 |
const link = getSelectedLink(editor);
|
|
|
123 |
if (link) {
|
|
|
124 |
const url = link.getAttribute('href');
|
|
|
125 |
const target = link.getAttribute('target');
|
|
|
126 |
const textToDisplay = link.innerText;
|
|
|
127 |
const title = link.getAttribute('title');
|
|
|
128 |
|
|
|
129 |
if (url !== '') {
|
|
|
130 |
properties.url = url;
|
|
|
131 |
}
|
|
|
132 |
if (target === '_blank') {
|
|
|
133 |
properties.newwindow = true;
|
|
|
134 |
}
|
|
|
135 |
if (title && title !== '') {
|
|
|
136 |
properties.urltext = title.trim();
|
|
|
137 |
} else if (textToDisplay !== '') {
|
|
|
138 |
properties.urltext = textToDisplay.trim();
|
|
|
139 |
}
|
|
|
140 |
} else {
|
|
|
141 |
// Check if the user is selecting some text before clicking on the Link button.
|
|
|
142 |
const selectedNode = editor.selection.getNode();
|
|
|
143 |
if (selectedNode) {
|
|
|
144 |
const textToDisplay = getTextSelection(editor);
|
|
|
145 |
if (textToDisplay !== '') {
|
|
|
146 |
properties.urltext = textToDisplay.trim();
|
|
|
147 |
properties.hasTextToDisplay = true;
|
|
|
148 |
properties.hasPlainTextSelected = true;
|
|
|
149 |
} else {
|
|
|
150 |
if (selectedNode.getAttribute('data-mce-selected')) {
|
|
|
151 |
properties.setLinkOnElement = true;
|
|
|
152 |
}
|
|
|
153 |
}
|
|
|
154 |
}
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
return properties;
|
|
|
158 |
};
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* Get selected link.
|
|
|
162 |
*
|
|
|
163 |
* @param {TinyMCE} editor
|
|
|
164 |
* @returns {Element}
|
|
|
165 |
*/
|
|
|
166 |
const getSelectedLink = (editor) => {
|
|
|
167 |
return getAnchorElement(editor);
|
|
|
168 |
};
|
|
|
169 |
|
|
|
170 |
/**
|
|
|
171 |
* Get anchor element.
|
|
|
172 |
*
|
|
|
173 |
* @param {TinyMCE} editor
|
|
|
174 |
* @param {Element} selectedElm
|
|
|
175 |
* @returns {Element}
|
|
|
176 |
*/
|
|
|
177 |
const getAnchorElement = (editor, selectedElm) => {
|
|
|
178 |
selectedElm = selectedElm || editor.selection.getNode();
|
|
|
179 |
return editor.dom.getParent(selectedElm, 'a[href]');
|
|
|
180 |
};
|
|
|
181 |
|
|
|
182 |
/**
|
|
|
183 |
* Get only the selected text.
|
|
|
184 |
* In some cases, window.getSelection() is not run as expected. We should only get the text value
|
|
|
185 |
* For ex: <img src="" alt="XYZ">Some text here
|
|
|
186 |
* window.getSelection() will return XYZSome text here
|
|
|
187 |
*
|
|
|
188 |
* @param {TinyMCE} editor
|
|
|
189 |
* @return {string} Selected text
|
|
|
190 |
*/
|
|
|
191 |
const getTextSelection = (editor) => {
|
|
|
192 |
let selText = '';
|
|
|
193 |
const sel = editor.selection.getSel();
|
|
|
194 |
const rangeCount = sel.rangeCount;
|
|
|
195 |
if (rangeCount) {
|
|
|
196 |
let rangeTexts = [];
|
|
|
197 |
for (let i = 0; i < rangeCount; ++i) {
|
|
|
198 |
rangeTexts.push('' + sel.getRangeAt(i));
|
|
|
199 |
}
|
|
|
200 |
selText = rangeTexts.join('');
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
return selText;
|
|
|
204 |
};
|
|
|
205 |
|
|
|
206 |
/**
|
|
|
207 |
* Check the current selected element is an anchor or not.
|
|
|
208 |
*
|
|
|
209 |
* @param {TinyMCE} editor
|
|
|
210 |
* @param {Element} selectedElm
|
|
|
211 |
* @returns {boolean}
|
|
|
212 |
*/
|
|
|
213 |
const isInAnchor = (editor, selectedElm) => getAnchorElement(editor, selectedElm) !== null;
|
|
|
214 |
|
|
|
215 |
/**
|
|
|
216 |
* Change state of button.
|
|
|
217 |
*
|
|
|
218 |
* @param {TinyMCE} editor
|
|
|
219 |
* @param {function()} toggler
|
|
|
220 |
* @returns {function()}
|
|
|
221 |
*/
|
|
|
222 |
const toggleState = (editor, toggler) => {
|
|
|
223 |
editor.on('NodeChange', toggler);
|
|
|
224 |
return () => editor.off('NodeChange', toggler);
|
|
|
225 |
};
|
|
|
226 |
|
|
|
227 |
/**
|
|
|
228 |
* Change the active state of button.
|
|
|
229 |
*
|
|
|
230 |
* @param {TinyMCE} editor
|
|
|
231 |
* @returns {function(*): function(): *}
|
|
|
232 |
*/
|
|
|
233 |
export const toggleActiveState = (editor) => (api) => {
|
|
|
234 |
const updateState = () => api.setActive(!editor.mode.isReadOnly() && isInAnchor(editor, editor.selection.getNode()));
|
|
|
235 |
updateState();
|
|
|
236 |
return toggleState(editor, updateState);
|
|
|
237 |
};
|