Skip to content

Commit 4e6b74f

Browse files
committed
WYSIWYG: Added filtering of page pointer elements
For #4474
1 parent c6d0e69 commit 4e6b74f

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

resources/js/wysiwyg/config.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {scrollToQueryString} from './scrolling';
44
import {listenForDragAndPaste} from './drop-paste-handling';
55
import {getPrimaryToolbar, registerAdditionalToolbars} from './toolbars';
66
import {registerCustomIcons} from './icons';
7+
import {setupFilters} from './filters';
78

89
import {getPlugin as getCodeeditorPlugin} from './plugin-codeeditor';
910
import {getPlugin as getDrawioPlugin} from './plugin-drawio';
@@ -147,23 +148,6 @@ function fetchCustomHeadContent() {
147148
return headContentLines.slice(startLineIndex + 1, endLineIndex).join('\n');
148149
}
149150

150-
/**
151-
* Setup a serializer filter for <br> tags to ensure they're not rendered
152-
* within code blocks and that we use newlines there instead.
153-
* @param {Editor} editor
154-
*/
155-
function setupBrFilter(editor) {
156-
editor.serializer.addNodeFilter('br', nodes => {
157-
for (const node of nodes) {
158-
if (node.parent && node.parent.name === 'code') {
159-
const newline = window.tinymce.html.Node.create('#text');
160-
newline.value = '\n';
161-
node.replace(newline);
162-
}
163-
}
164-
});
165-
}
166-
167151
/**
168152
* @param {WysiwygConfigOptions} options
169153
* @return {function(Editor)}
@@ -189,7 +173,7 @@ function getSetupCallback(options) {
189173
});
190174

191175
editor.on('PreInit', () => {
192-
setupBrFilter(editor);
176+
setupFilters(editor);
193177
});
194178

195179
// Custom handler hook

resources/js/wysiwyg/filters.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Setup a serializer filter for <br> tags to ensure they're not rendered
3+
* within code blocks and that we use newlines there instead.
4+
* @param {Editor} editor
5+
*/
6+
function setupBrFilter(editor) {
7+
editor.serializer.addNodeFilter('br', nodes => {
8+
for (const node of nodes) {
9+
if (node.parent && node.parent.name === 'code') {
10+
const newline = window.tinymce.html.Node.create('#text');
11+
newline.value = '\n';
12+
node.replace(newline);
13+
}
14+
}
15+
});
16+
}
17+
18+
/**
19+
* Remove accidentally added pointer elements that are within the content.
20+
* These could have accidentally been added via getting caught in range
21+
* selection within page content.
22+
* @param {Editor} editor
23+
*/
24+
function setupPointerFilter(editor) {
25+
editor.parser.addNodeFilter('div', nodes => {
26+
for (const node of nodes) {
27+
if (node.attr('id') === 'pointer' || node.attr('class').includes('pointer')) {
28+
node.remove();
29+
}
30+
}
31+
});
32+
}
33+
34+
/**
35+
* Setup global default filters for the given editor instance.
36+
* @param {Editor} editor
37+
*/
38+
export function setupFilters(editor) {
39+
setupBrFilter(editor);
40+
setupPointerFilter(editor);
41+
}

0 commit comments

Comments
 (0)