-
Notifications
You must be signed in to change notification settings - Fork 953
Expand file tree
/
Copy pathtinyMCE.js
More file actions
98 lines (82 loc) · 2.91 KB
/
tinyMCE.js
File metadata and controls
98 lines (82 loc) · 2.91 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
import { markers, languageProcessing } from "yoastseo";
import { forEach } from "lodash-es";
var MARK_TAG = "yoastmark";
/**
* Cleans the editor of any invalid marks. Invalid marks are marks where < and > are converted to
* html entities by tinyMCE so these can be filtered out to keep the output text clean.
*
* @param {tinyMCE.Editor} editor The editor to remove invalid marks from.
*
* @returns {void}
*/
function removeInvalidMarks( editor ) {
var html = editor.getContent();
html = html
.replace( new RegExp( "<yoastmark.+?>", "g" ), "" )
.replace( new RegExp( "</yoastmark>", "g" ), "" );
editor.setContent( html );
}
/**
* Puts a list of marks into the given tinyMCE editor
*
* @param {tinyMCE.Editor} editor The editor to apply the marks to.
* @param {Paper} paper The paper for which the marks have been generated.
* @param {Array.<Mark>} marks The marks to show in the editor.
*
* @returns {void}
*/
function markTinyMCE( editor, paper, marks ) {
var dom = editor.dom;
var html = editor.getContent();
html = markers.removeMarks( html );
// Generate marked HTML.
forEach( marks, function( mark ) {
mark._properties.marked = languageProcessing.replaceSingleQuotesInTags( mark._properties.marked );
mark._properties.original = languageProcessing.replaceSingleQuotesInTags( mark._properties.original );
html = mark.applyWithReplace( html );
} );
// Replace the contents in the editor with the marked HTML.
editor.setContent( html );
removeInvalidMarks( editor );
var markElements = dom.select( MARK_TAG );
/*
* The `mce-bogus` data is an internal tinyMCE indicator that the elements themselves shouldn't be saved.
* Add data-mce-bogus after the elements have been inserted because setContent strips elements with data-mce-bogus.
*/
forEach( markElements, function( markElement ) {
markElement.setAttribute( "data-mce-bogus", "1" );
} );
}
/**
* Returns a function that can decorate a tinyMCE editor
*
* @param {tinyMCE.Editor} editor The editor to return a function for.
* @returns {Function} The function that can be called to decorate the editor.
*/
export function tinyMCEDecorator( editor ) {
window.test = editor;
return markTinyMCE.bind( null, editor );
}
/**
* Returns whether or not the editor has marks
*
* @param {tinyMCE.Editor} editor The editor.
* @returns {boolean} Whether or not there are marks inside the editor.
*/
export function editorHasMarks( editor ) {
var content = editor.getContent( { format: "raw" } );
return -1 !== content.indexOf( "<" + MARK_TAG );
}
/**
* Removes marks currently in the given editor
*
* @param {tinyMCE.Editor} editor The editor to remove all marks for.
*
* @returns {void}
*/
export function editorRemoveMarks( editor ) {
// Create a decorator with the given editor.
var decorator = tinyMCEDecorator( editor );
// Calling the decorator with an empty array of marks will clear the editor of marks.
decorator( null, [] );
}