-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinymce-vue.js
311 lines (296 loc) · 9.38 KB
/
tinymce-vue.js
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
var Editor = (function () {
'use strict';
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
/* global Reflect, Promise */
var __assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
/**
* Copyright (c) 2018-present, Ephox, Inc.
*
* This source code is licensed under the Apache 2 license found in the
* LICENSE file in the root directory of this source tree.
*
*/
var validEvents = [
'onActivate',
'onAddUndo',
'onBeforeAddUndo',
'onBeforeExecCommand',
'onBeforeGetContent',
'onBeforeRenderUI',
'onBeforeSetContent',
'onBeforePaste',
'onBlur',
'onChange',
'onClearUndos',
'onClick',
'onContextMenu',
'onCopy',
'onCut',
'onDblclick',
'onDeactivate',
'onDirty',
'onDrag',
'onDragDrop',
'onDragEnd',
'onDragGesture',
'onDragOver',
'onDrop',
'onExecCommand',
'onFocus',
'onFocusIn',
'onFocusOut',
'onGetContent',
'onHide',
'onInit',
'onKeyDown',
'onKeyPress',
'onKeyUp',
'onLoadContent',
'onMouseDown',
'onMouseEnter',
'onMouseLeave',
'onMouseMove',
'onMouseOut',
'onMouseOver',
'onMouseUp',
'onNodeChange',
'onObjectResizeStart',
'onObjectResized',
'onObjectSelected',
'onPaste',
'onPostProcess',
'onPostRender',
'onPreProcess',
'onProgressState',
'onRedo',
'onRemove',
'onReset',
'onSaveContent',
'onSelectionChange',
'onSetAttrib',
'onSetContent',
'onShow',
'onSubmit',
'onUndo',
'onVisualAid'
];
var isValidKey = function (key) { return validEvents.indexOf(key) !== -1; };
var bindHandlers = function (initEvent, listeners, editor) {
Object.keys(listeners)
.filter(isValidKey)
.forEach(function (key) {
var handler = listeners[key];
if (typeof handler === 'function') {
if (key === 'onInit') {
handler(initEvent, editor);
}
else {
editor.on(key.substring(2), function (e) { return handler(e, editor); });
}
}
});
};
var bindModelHandlers = function (ctx, editor) {
var modelEvents = ctx.$props.modelEvents ? ctx.$props.modelEvents : null;
var normalizedEvents = Array.isArray(modelEvents) ? modelEvents.join(' ') : modelEvents;
var currentContent;
ctx.$watch('value', function (val, prevVal) {
if (editor && typeof val === 'string' && val !== currentContent && val !== prevVal) {
editor.setContent(val);
currentContent = val;
}
});
editor.on(normalizedEvents ? normalizedEvents : 'change keyup undo redo', function () {
currentContent = editor.getContent();
ctx.$emit('input', currentContent);
});
};
var initEditor = function (initEvent, ctx, editor) {
var value = ctx.$props.value ? ctx.$props.value : '';
var initialValue = ctx.$props.initialValue ? ctx.$props.initialValue : '';
editor.setContent(value || initialValue);
// checks if the v-model shorthand is used (which sets an v-on:input listener) and then binds either
// specified the events or defaults to "change keyup" event and emits the editor content on that event
if (ctx.$listeners.input) {
bindModelHandlers(ctx, editor);
}
bindHandlers(initEvent, ctx.$listeners, editor);
};
var unique = 0;
var uuid = function (prefix) {
var date = new Date();
var time = date.getTime();
var random = Math.floor(Math.random() * 1000000000);
unique++;
return prefix + '_' + random + unique + String(time);
};
var isTextarea = function (element) {
return element !== null && element.tagName.toLowerCase() === 'textarea';
};
var normalizePluginArray = function (plugins) {
if (typeof plugins === 'undefined' || plugins === '') {
return [];
}
return Array.isArray(plugins) ? plugins : plugins.split(' ');
};
var mergePlugins = function (initPlugins, inputPlugins) {
return normalizePluginArray(initPlugins).concat(normalizePluginArray(inputPlugins));
};
/**
* Copyright (c) 2018-present, Ephox, Inc.
*
* This source code is licensed under the Apache 2 license found in the
* LICENSE file in the root directory of this source tree.
*
*/
var injectScriptTag = function (scriptId, doc, url, callback) {
var scriptTag = doc.createElement('script');
scriptTag.type = 'application/javascript';
scriptTag.id = scriptId;
scriptTag.addEventListener('load', callback);
scriptTag.src = url;
doc.head.appendChild(scriptTag);
};
var create = function () {
return {
listeners: [],
scriptId: uuid('tiny-script'),
scriptLoaded: false
};
};
var load = function (state, doc, url, callback) {
if (state.scriptLoaded) {
callback();
}
else {
state.listeners.push(callback);
if (!doc.getElementById(state.scriptId)) {
injectScriptTag(state.scriptId, doc, url, function () {
state.listeners.forEach(function (fn) { return fn(); });
state.scriptLoaded = true;
});
}
}
};
/**
* Copyright (c) 2018-present, Ephox, Inc.
*
* This source code is licensed under the Apache 2 license found in the
* LICENSE file in the root directory of this source tree.
*
*/
var getGlobal = function () { return (typeof window !== 'undefined' ? window : global); };
var getTinymce = function () {
var global = getGlobal();
return global && global.tinymce ? global.tinymce : null;
};
/**
* Copyright (c) 2018-present, Ephox, Inc.
*
* This source code is licensed under the Apache 2 license found in the
* LICENSE file in the root directory of this source tree.
*
*/
var editorProps = {
// apiKey: String,
cloudChannel: String,
id: String,
init: Object,
initialValue: String,
inline: Boolean,
modelEvents: [String, Array],
plugins: [String, Array],
tagName: String,
toolbar: [String, Array],
value: String,
url:String
};
/**
* Copyright (c) 2018-present, Ephox, Inc.
*
* This source code is licensed under the Apache 2 license found in the
* LICENSE file in the root directory of this source tree.
*
*/
var scriptState = create();
var renderInline = function (h, id, tagName) {
return h(tagName ? tagName : 'div', {
attrs: { id: id }
});
};
var renderIframe = function (h, id) {
return h('textarea', {
attrs: { id: id },
style: { visibility: 'hidden' }
});
};
var initialise = function (ctx) { return function () {
var finalInit = __assign({}, ctx.$props.init, { selector: "#" + ctx.elementId, plugins: mergePlugins(ctx.$props.init && ctx.$props.init.plugins, ctx.$props.plugins), toolbar: ctx.$props.toolbar || (ctx.$props.init && ctx.$props.init.toolbar), inline: ctx.inlineEditor, setup: function (editor) {
ctx.editor = editor;
editor.on('init', function (e) { return initEditor(e, ctx, editor); });
if (ctx.$props.init && typeof ctx.$props.init.setup === 'function') {
ctx.$props.init.setup(editor);
}
} });
if (isTextarea(ctx.element)) {
ctx.element.style.visibility = '';
}
// console.log(getTinymce());
getTinymce().init(finalInit);
}; };
var Editor = {
props: editorProps,
created: function () {
this.elementId = this.$props.id || uuid('tiny-vue');
this.inlineEditor = (this.$props.init && this.$props.init.inline) || this.$props.inline;
},
mounted: function () {
this.element = this.$el;
if (getTinymce() !== null) {
initialise(this)();
}
else if (this.element) {
var doc = this.element.ownerDocument;
var channel = this.$props.cloudChannel ? this.$props.cloudChannel : 'stable';
// var apiKey = this.$props.apiKey ? this.$props.apiKey : '';
// var url = "https://cloud.tinymce.com/" + channel + "/tinymce.min.js?apiKey=" + apiKey;
var url = this.$props.url;
load(scriptState, doc, url, initialise(this));
}
},
beforeDestroy: function () {
if (getTinymce() !== null) {
getTinymce().remove(this.editor);
}
},
render: function (h) {
return this.inlineEditor ? renderInline(h, this.elementId, this.$props.tagName) : renderIframe(h, this.elementId);
}
};
/**
* Copyright (c) 2018-present, Ephox, Inc.
*
* This source code is licensed under the Apache 2 license found in the
* LICENSE file in the root directory of this source tree.
*
*/
return Editor;
}());
export default Editor;