UNPKG

6.13 kBJavaScriptView Raw
1"use strict";
2/**
3 * Copyright (c) 2018-present, Ephox, Inc.
4 *
5 * This source code is licensed under the Apache 2 license found in the
6 * LICENSE file in the root directory of this source tree.
7 *
8 */
9var __assign = (this && this.__assign) || function () {
10 __assign = Object.assign || function(t) {
11 for (var s, i = 1, n = arguments.length; i < n; i++) {
12 s = arguments[i];
13 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
14 t[p] = s[p];
15 }
16 return t;
17 };
18 return __assign.apply(this, arguments);
19};
20Object.defineProperty(exports, "__esModule", { value: true });
21exports.Editor = void 0;
22var ScriptLoader_1 = require("../ScriptLoader");
23var TinyMCE_1 = require("../TinyMCE");
24var Utils_1 = require("../Utils");
25var EditorPropTypes_1 = require("./EditorPropTypes");
26var vue_1 = require("vue");
27var renderInline = function (ce, id, elementRef, tagName) {
28 return ce(tagName ? tagName : 'div', {
29 id: id,
30 ref: elementRef
31 });
32};
33var renderIframe = function (ce, id, elementRef) {
34 return ce('textarea', {
35 id: id,
36 visibility: 'hidden',
37 ref: elementRef
38 });
39};
40var defaultInitValues = { selector: undefined, target: undefined };
41exports.Editor = (0, vue_1.defineComponent)({
42 props: EditorPropTypes_1.editorProps,
43 setup: function (props, ctx) {
44 var conf = props.init ? __assign(__assign({}, props.init), defaultInitValues) : __assign({}, defaultInitValues);
45 var _a = (0, vue_1.toRefs)(props), disabled = _a.disabled, modelValue = _a.modelValue, tagName = _a.tagName;
46 var element = (0, vue_1.ref)(null);
47 var vueEditor = null;
48 var elementId = props.id || (0, Utils_1.uuid)('tiny-vue');
49 var inlineEditor = (props.init && props.init.inline) || props.inline;
50 var modelBind = !!ctx.attrs['onUpdate:modelValue'];
51 var mounting = true;
52 var initialValue = props.initialValue ? props.initialValue : '';
53 var cache = '';
54 var getContent = function (isMounting) { return modelBind ?
55 function () { return ((modelValue === null || modelValue === void 0 ? void 0 : modelValue.value) ? modelValue.value : ''); } :
56 function () { return isMounting ? initialValue : cache; }; };
57 var initWrapper = function () {
58 var content = getContent(mounting);
59 var finalInit = __assign(__assign({}, conf), { readonly: props.disabled, target: element.value, plugins: (0, Utils_1.mergePlugins)(conf.plugins, props.plugins), toolbar: props.toolbar || (conf.toolbar), inline: inlineEditor, setup: function (editor) {
60 vueEditor = editor;
61 editor.on('init', function (e) { return (0, Utils_1.initEditor)(e, props, ctx, editor, modelValue, content); });
62 if (typeof conf.setup === 'function') {
63 conf.setup(editor);
64 }
65 } });
66 if ((0, Utils_1.isTextarea)(element.value)) {
67 element.value.style.visibility = '';
68 }
69 (0, TinyMCE_1.getTinymce)().init(finalInit);
70 mounting = false;
71 };
72 (0, vue_1.watch)(disabled, function (disable) {
73 var _a;
74 if (vueEditor !== null) {
75 if (typeof ((_a = vueEditor.mode) === null || _a === void 0 ? void 0 : _a.set) === 'function') {
76 vueEditor.mode.set(disable ? 'readonly' : 'design');
77 }
78 else {
79 vueEditor.setMode(disable ? 'readonly' : 'design');
80 }
81 }
82 });
83 (0, vue_1.watch)(tagName, function (_) {
84 var _a;
85 if (!modelBind) {
86 cache = vueEditor.getContent();
87 }
88 (_a = (0, TinyMCE_1.getTinymce)()) === null || _a === void 0 ? void 0 : _a.remove(vueEditor);
89 (0, vue_1.nextTick)(function () { return initWrapper(); });
90 });
91 (0, vue_1.onMounted)(function () {
92 if ((0, TinyMCE_1.getTinymce)() !== null) {
93 initWrapper();
94 }
95 else if (element.value && element.value.ownerDocument) {
96 var channel = props.cloudChannel ? props.cloudChannel : '7';
97 var apiKey = props.apiKey ? props.apiKey : 'no-api-key';
98 var scriptSrc = (0, Utils_1.isNullOrUndefined)(props.tinymceScriptSrc) ?
99 "https://cdn.tiny.cloud/1/".concat(apiKey, "/tinymce/").concat(channel, "/tinymce.min.js") :
100 props.tinymceScriptSrc;
101 ScriptLoader_1.ScriptLoader.load(element.value.ownerDocument, scriptSrc, initWrapper);
102 }
103 });
104 (0, vue_1.onBeforeUnmount)(function () {
105 if ((0, TinyMCE_1.getTinymce)() !== null) {
106 (0, TinyMCE_1.getTinymce)().remove(vueEditor);
107 }
108 });
109 if (!inlineEditor) {
110 (0, vue_1.onActivated)(function () {
111 if (!mounting) {
112 initWrapper();
113 }
114 });
115 (0, vue_1.onDeactivated)(function () {
116 var _a;
117 if (!modelBind) {
118 cache = vueEditor.getContent();
119 }
120 (_a = (0, TinyMCE_1.getTinymce)()) === null || _a === void 0 ? void 0 : _a.remove(vueEditor);
121 });
122 }
123 var rerender = function (init) {
124 var _a;
125 cache = vueEditor.getContent();
126 (_a = (0, TinyMCE_1.getTinymce)()) === null || _a === void 0 ? void 0 : _a.remove(vueEditor);
127 conf = __assign(__assign(__assign({}, conf), init), defaultInitValues);
128 (0, vue_1.nextTick)(function () { return initWrapper(); });
129 };
130 ctx.expose({
131 rerender: rerender,
132 getEditor: function () { return vueEditor; }
133 });
134 return function () { return inlineEditor ?
135 renderInline(vue_1.h, elementId, element, props.tagName) :
136 renderIframe(vue_1.h, elementId, element); };
137 }
138});