-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathtinymce.js
More file actions
169 lines (157 loc) · 5.43 KB
/
tinymce.js
File metadata and controls
169 lines (157 loc) · 5.43 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
// Import TinyMCE
import tinymce from 'tinymce/tinymce';
// TinyMCE DOM helpers
import 'tinymce/models/dom/';
// TinyMCE toolbar icons
import 'tinymce/icons/default';
// TinyMCE theme
import 'tinymce/themes/silver';
// TinyMCE Plugins
import 'tinymce/plugins/table';
import 'tinymce/plugins/lists';
import 'tinymce/plugins/autoresize';
import 'tinymce/plugins/link';
import 'tinymce/plugins/advlist';
import 'tinymce/plugins/autolink';
// Other dependencies
import { isObject, isString, isUndefined } from './isType';
// // Configuration extracted from
// // https://www.tinymce.com/docs/advanced/usage-with-module-loaders/
export const defaultOptions = {
selector: '.tinymce',
license_key: 'gpl',
statusbar: true,
menubar: false,
toolbar: 'bold italic | bullist numlist | link | table',
plugins: 'table autoresize link advlist lists autolink',
browser_spellcheck: true,
advlist_bullet_styles: 'circle,disc,square', // Only disc bullets display on htmltoword
target_list: false,
elementpath: false,
resize: true,
min_height: 230,
autoresize_bottom_margin: 10,
branding: false,
extended_valid_elements: 'iframe[tooltip] , a[href|target=_blank]',
paste_as_text: false,
paste_block_drop: true,
paste_merge_formats: true,
paste_tab_spaces: 4,
smart_paste: true,
paste_data_images: true,
paste_remove_styles_if_webkit: true,
paste_webkit_styles: 'none',
table_default_attributes: {
border: 1,
},
// editorManager.baseURL is not resolved properly for IE since document.currentScript
// is not supported, see issue https://github.com/tinymce/tinymce/issues/358
skin_url: '/tinymce/skins/oxide',
content_css: ['/tinymce/tinymce.css'],
};
/*
This function determines whether or not the editor is a TinyMCE editor
*/
const isTinymceEditor = (editor) => {
if (isObject(editor)) {
return editor.hasOwnProperty('id') && typeof editor.getContainer === 'function';
} else {
return false;
}
};
/*
This function is invoked after the Tinymce widget is initialized. It moves the
connection with the label from the hidden field (that the Tinymce writes to
behind the scenes) to the Tinymce iframe so that screen readers read the correct
label when the tinymce iframe receives focus.
*/
const attachLabelToIframe = (editor) => {
if (isTinymceEditor(editor)) {
const iframe = editor.getContainer().querySelector('iframe');
const lbl = document.querySelector(`label[for="${editor.id}"]`);
// If the iframe and label could be found, then set the label's 'for' attribute to the id of the iframe
if (isObject(iframe) && isObject(lbl)) {
lbl.setAttribute('for', iframe.getAttribute('id'));
}
}
};
export const Tinymce = {
/*
Initialises a tinymce editor given the object passed. If a non-valid object is passed,
the defaultOptions object is used instead
@param options - An object with tinyMCE properties
*/
init(options = {}) {
// If any options were specified, merge them with the default options.
let opts = {
...defaultOptions,
...options
};
tinymce.init(opts).then((editors) => {
if (editors.length > 0) {
for (const editor of editors) {
// auto-resize the editor and connect the form label to the TinyMCE iframe
editor.execCommand('mceAutoResize');
attachLabelToIframe(editor, editor.id);
}
}
});
},
/*
Finds any tinyMCE editor whose target element/textarea has the className passed
@param className - A string representing the class name of the tinyMCE editor
target element/textarea to look for
@return An Array of tinymce.Editor objects
*/
findEditorsByClassName(className) {
if (isString(className)) {
const elements = Array.from(document.getElementsByClassName(className));
// Fetch the textarea elements and then return the TinyMCE editors associated with the element ids
return elements.map((el) => {
return Tinymce.findEditorById(el.getAttribute('id'));
});
}
return [];
},
/*
Finds a tinyMCE editor whose target element/textarea has the id passed
@param id - A string representing the id of the tinyMCE editor target
element/textarea to look for
@return tinymce.Editor object, otherwise undefined
*/
findEditorById(id) {
if (isString(id)) {
return tinymce.get(id);
}
return undefined;
},
/*
Destroy every editor instance whose target element/textarea has the className passed. This
method executes for each editor the method defined at tinymce.Editor.destroy (e.g. https://www.tinymce.com/docs/api/tinymce/tinymce.editor/#destroy).
@param className - A string representing the class name of the tinyMCE editor
target element/textarea to look for
@return undefined
*/
destroyEditorsByClassName(className) {
const editors = this.findEditorsByClassName(className);
if (editors.length > 0) {
/* editors.forEach(ed => ed.destroy(false)); */
for (const editor of editors) {
if (isTinymceEditor(editor)) {
editor.destroy(false);
}
}
}
},
/*
Destroy an editor instance whose target element/textarea has HTML id passed. This method
executes tinymce.Editor.destroy (e.g. https://www.tinymce.com/docs/api/tinymce/tinymce.editor/#destroy) for a successfull id found.
@return undefined
*/
destroyEditorById(id) {
const editor = this.findEditorById(id);
if (isTinymceEditor(editor)) {
editor.destroy(false);
}
},
};