forked from JiHong88/suneditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink.js
More file actions
273 lines (242 loc) · 7.81 KB
/
link.js
File metadata and controls
273 lines (242 loc) · 7.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import { PluginModal } from '../../interfaces';
import { Modal, Controller } from '../../modules/contract';
import { ModalAnchorEditor } from '../../modules/ui';
import { dom, numbers } from '../../helper';
/**
* @typedef {Object} LinkOptions
* @property {string} [uploadUrl] - The URL endpoint for file uploads.
* - The server must return:
* ```js
* {
* "result": [
* {
* "url": "https://example.com/file.pdf",
* "name": "file.pdf",
* "size": 1048576
* }
* ]
* }
* ```
* @property {Object<string, string>} [uploadHeaders] - Additional headers for file upload requests.
* @property {number} [uploadSizeLimit] - The total file upload size limit in bytes.
* @property {number} [uploadSingleSizeLimit] - The single file upload size limit in bytes.
* @property {string} [acceptedFormats] - Accepted file formats for link uploads.
*/
/**
* @typedef {Omit<LinkOptions & import('../../modules/ui/ModalAnchorEditor').ModalAnchorEditorParams, ''>} LinkPluginOptions
*/
/**
* @class
* @description Link plugin.
* - This plugin provides link insertion and editing functionality within the editor.
* - It also supports file uploads if an upload URL is provided.
*/
class Link extends PluginModal {
static key = 'link';
static className = 'se-icon-flip-rtl';
#controllerATarget;
/**
* @constructor
* @param {SunEditor.Kernel} kernel - The Kernel instance
* @param {LinkPluginOptions} pluginOptions
*/
constructor(kernel, pluginOptions) {
// plugin bisic properties
super(kernel);
this.title = this.$.lang.link;
this.icon = 'link';
// define plugin options
pluginOptions.textToDisplay = true;
pluginOptions.title = true;
// create HTML
const modalEl = CreateHTML_modal(this.$);
const controllerEl = CreateHTML_controller(this.$);
// members
const uploadUrl = typeof pluginOptions.uploadUrl === 'string' ? pluginOptions.uploadUrl : null;
this.target = null;
this.isUpdateState = false;
this.pluginOptions = {
...pluginOptions,
uploadUrl,
uploadHeaders: pluginOptions.uploadHeaders || null,
uploadSizeLimit: numbers.get(pluginOptions.uploadSizeLimit, 0),
uploadSingleSizeLimit: numbers.get(pluginOptions.uploadSingleSizeLimit, 0),
acceptedFormats: typeof pluginOptions.acceptedFormats === 'string' ? pluginOptions.acceptedFormats.trim() : null,
enableFileUpload: !!uploadUrl,
};
// modules
this.anchor = new ModalAnchorEditor(this.$, modalEl, this.pluginOptions);
this.modal = new Modal(this, this.$, modalEl);
this.controller = new Controller(this, this.$, controllerEl, { position: 'bottom', disabled: false });
this.#controllerATarget = this.controller.form.querySelector('a');
}
/**
* @hook Editor.EventManager
* @type {SunEditor.Hook.Event.Active}
*/
active(element) {
if (dom.check.isAnchor(element) && !element.hasAttribute('data-se-non-link')) {
const a = this.#controllerATarget;
const href = element.getAttribute('href');
a.href = href;
a.textContent = a.title = element.textContent;
a.target = href?.startsWith('#') ? element.target : '_blank';
dom.utils.addClass(element, 'on');
this.anchor.set(element);
this.controller.open(element, null, { isWWTarget: false, initMethod: null, addOffset: null });
this.target = element;
return true;
}
this.controller.close();
return false;
}
/**
* @override
* @type {PluginModal['open']}
*/
open() {
this.modal.open();
}
/**
* @hook Modules.Modal
* @type {SunEditor.Hook.Modal.On}
*/
modalOn(isUpdate) {
this.isUpdateState = isUpdate;
this.anchor.on(isUpdate);
}
/**
* @hook Modules.Modal
* @type {SunEditor.Hook.Modal.Action}
*/
async modalAction() {
const oA = this.anchor.create(false);
if (oA === null) return false;
if (!this.isUpdateState) {
const selectedFormats = this.$.format.getLines();
if (selectedFormats.length > 1) {
if (!this.$.html.insertNode(dom.utils.createElement(selectedFormats[0].nodeName, null, oA), { afterNode: null, skipCharCount: false })) return true;
} else {
if (!this.$.html.insertNode(oA, { afterNode: null, skipCharCount: false })) return true;
}
this.$.selection.setRange(oA.childNodes[0], 0, oA.childNodes[0], oA.textContent.length);
} else {
// set range
const textNode = this.controller.currentTarget.childNodes[0];
this.$.selection.setRange(textNode, 0, textNode, textNode.textContent.length);
}
this.$.history.push(false);
return true;
}
/**
* @hook Modules.Modal
* @type {SunEditor.Hook.Modal.Init}
*/
modalInit() {
this.controller.close();
this.anchor.init();
}
/**
* @hook Modules.Controller
* @type {SunEditor.Hook.Controller.Action}
*/
controllerAction(target) {
const command = target.getAttribute('data-command');
if (/update/.test(command)) {
this.modal.open();
} else if (/copy/.test(command)) {
this.$.html.copy(this.target);
} else if (/unlink/.test(command)) {
const sc = dom.query.getEdgeChild(
this.controller.currentTarget,
function (current) {
return current.childNodes.length === 0 || current.nodeType === 3;
},
false,
);
const ec = dom.query.getEdgeChild(
this.controller.currentTarget,
function (current) {
return current.childNodes.length === 0 || current.nodeType === 3;
},
true,
);
this.$.selection.setRange(sc, 0, ec, ec.textContent.length);
this.$.inline.apply(null, { stylesToModify: null, nodesToRemove: ['A'], strictRemove: false });
} else {
/** delete */
dom.utils.removeItem(this.controller.currentTarget);
this.controller.currentTarget = null;
this.$.focusManager.focus();
this.$.history.push(false);
}
}
/**
* @hook Modules.Controller
* @type {SunEditor.Hook.Controller.Close}
*/
controllerClose() {
dom.utils.removeClass(this.controller.currentTarget, 'on');
}
}
/**
* @param {SunEditor.Deps} $ - Kernel dependencies
* @returns {HTMLElement}
*/
function CreateHTML_modal({ lang, icons }) {
const html = /*html*/ `
<form>
<div class="se-modal-header">
<button type="button" data-command="close" class="se-btn se-close-btn" title="${lang.close}" aria-label="${lang.close}">
${icons.cancel}
</button>
<span class="se-modal-title">${lang.link_modal_title}</span>
</div>
<div class="se-anchor-editor"></div>
<div class="se-modal-footer">
<button type="submit" class="se-btn-primary" title="${lang.submitButton}" aria-label="${lang.submitButton}">
<span>${lang.submitButton}</span>
</button>
</div>
</form>`;
return dom.utils.createElement('DIV', { class: 'se-modal-content' }, html);
}
/**
* @param {SunEditor.Deps} $ - Kernel dependencies
* @returns {HTMLElement}
*/
function CreateHTML_controller({ lang, icons }) {
const html = /*html*/ `
<div class="se-arrow se-arrow-up"></div>
<div class="link-content">
<span><a target="_blank" href=""></a> </span>
<div class="se-btn-group">
<button type="button" data-command="update" tabindex="-1" class="se-btn se-tooltip">
${icons.edit}
<span class="se-tooltip-inner">
<span class="se-tooltip-text">${lang.edit}</span>
</span>
</button>
<button type="button" data-command="copy" tabindex="-1" class="se-btn se-tooltip">
${icons.copy}
<span class="se-tooltip-inner">
<span class="se-tooltip-text">${lang.copy}</span>
</span>
</button>
<button type="button" data-command="unlink" tabindex="-1" class="se-btn se-tooltip se-icon-flip-rtl">
${icons.unlink}
<span class="se-tooltip-inner">
<span class="se-tooltip-text">${lang.unlink}</span>
</span>
</button>
<button type="button" data-command="delete" tabindex="-1" class="se-btn se-tooltip">
${icons.delete}
<span class="se-tooltip-inner">
<span class="se-tooltip-text">${lang.remove}</span>
</span>
</button>
</div>
</div>`;
return dom.utils.createElement('DIV', { class: 'se-controller se-controller-link' }, html);
}
export default Link;