Skip to content

Commit

Permalink
XWIKI-22750: Styles are not filtered anymore when pasting from LibreO…
Browse files Browse the repository at this point in the history
…ffice and Google Docs

* Apply the XWiki paste filter after the specialized LibreOffice and Google Docs one (when the configuration is on)
  • Loading branch information
mflorea committed Jan 9, 2025
1 parent 0dc8086 commit 86324b6
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,37 @@
}
},

afterInit: function(editor) {
if (editor.config.applyPasteFilterAfterPasteFromWord) {
const triggerAfterPasteFromWord = filteredWordContent => {
const data = {dataValue: filteredWordContent};
// This will apply the XWiki paste filter to the filtered content. See below.
editor.fire('afterPasteFromWord', data);
return data.dataValue;
};

// Make sure the XWiki paste filter is also applied when pasting from LibreOffice and Google Docs.
// See XWIKI-22750: Styles are not filtered anymore when pasting from LibreOffice
// The problem is that the LibreOffice and Google Docs paste filters are loaded and registered on demand, when
// the user pastes content from these tools, so we need to overwrite them when the paste event is triggered.
const targetPasteFilters = {'gdocs': false, 'libreoffice': false};
editor.on('paste', function(event) {
Object.keys(targetPasteFilters)
// Overwrite the target filters that have been loaded and are not already overwritten.
.filter(filterName => !targetPasteFilters[filterName] && CKEDITOR.pasteFilters?.[filterName])
.forEach(filterName => {
targetPasteFilters[filterName] = CKEDITOR.pasteFilters[filterName];
// Overwrite the target paste filter to trigger the 'afterPasteFromWord' event.
CKEDITOR.pasteFilters[filterName] = function(...args) {
const result = targetPasteFilters[filterName].apply(this, args);
return triggerAfterPasteFromWord(result);
};
});
// Our paste listener needs to be called before the pastetools one (which uses priority 3).
}, this, null, 2);
}
},

// The paste filter is not applied by default for content pasted from Word.
// https://dev.ckeditor.com/ticket/13093
// https://stackoverflow.com/questions/45501341/ckeditor-pastefromword-ignores-pastefilter
Expand Down

0 comments on commit 86324b6

Please sign in to comment.