From 7013be6825832cca31d07ecddd65d06f8cca922e Mon Sep 17 00:00:00 2001 From: Sauli Anto Date: Sun, 29 Sep 2019 04:14:02 +0300 Subject: [PATCH] Add paste from word base --- package-lock.json | 10 ++ package.json | 1 + src/math.js | 3 +- src/mathpastefromoffice.js | 263 +++++++++++++++++++++++++++++++++++++ 4 files changed, 276 insertions(+), 1 deletion(-) create mode 100644 src/mathpastefromoffice.js diff --git a/package-lock.json b/package-lock.json index 03734e959..84f74a7db 100644 --- a/package-lock.json +++ b/package-lock.json @@ -90,6 +90,16 @@ "@ckeditor/ckeditor5-undo": "^11.0.5" } }, + "@ckeditor/ckeditor5-paste-from-office": { + "version": "11.1.0", + "resolved": "https://registry.npmjs.org/@ckeditor/ckeditor5-paste-from-office/-/ckeditor5-paste-from-office-11.1.0.tgz", + "integrity": "sha512-LcXmJSlr0fzi+hccdyTnHuKh/K9nWV5OiuWqcOpX3IucczSYzGgykG4MUZVlBLv1mkP0AsjJ1oSPGXCkVkgJkg==", + "requires": { + "@ckeditor/ckeditor5-clipboard": "^12.0.2", + "@ckeditor/ckeditor5-core": "^12.3.0", + "@ckeditor/ckeditor5-engine": "^14.0.0" + } + }, "@ckeditor/ckeditor5-typing": { "version": "12.2.0", "resolved": "https://registry.npmjs.org/@ckeditor/ckeditor5-typing/-/ckeditor5-typing-12.2.0.tgz", diff --git a/package.json b/package.json index d2768c468..81cf29503 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "@ckeditor/ckeditor5-clipboard": "^12.0.2", "@ckeditor/ckeditor5-core": "^12.3.0", "@ckeditor/ckeditor5-engine": "^14.0.0", + "@ckeditor/ckeditor5-paste-from-office": "^11.1.0", "@ckeditor/ckeditor5-ui": "^14.0.0", "@ckeditor/ckeditor5-undo": "^11.0.5", "@ckeditor/ckeditor5-utils": "^14.0.0", diff --git a/src/math.js b/src/math.js index 73f753082..a828653c5 100644 --- a/src/math.js +++ b/src/math.js @@ -4,10 +4,11 @@ import Widget from '@ckeditor/ckeditor5-widget/src/widget'; import MathUI from './mathui'; import MathEditing from './mathediting'; import AutoMath from './automath'; +import MathPasteFromOffice from './mathpastefromoffice'; export default class Math extends Plugin { static get requires() { - return [ MathEditing, MathUI, Widget, AutoMath ]; + return [ MathEditing, MathUI, Widget, AutoMath, MathPasteFromOffice ]; } static get pluginName() { diff --git a/src/mathpastefromoffice.js b/src/mathpastefromoffice.js new file mode 100644 index 000000000..f7bc792fd --- /dev/null +++ b/src/mathpastefromoffice.js @@ -0,0 +1,263 @@ +import Plugin from '@ckeditor/ckeditor5-core/src/plugin'; +import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard'; +import UpcastWriter from '@ckeditor/ckeditor5-engine/src/view/upcastwriter'; +import Matcher from '@ckeditor/ckeditor5-engine/src/view/matcher'; +// import Element from '@ckeditor/ckeditor5-engine/src/view/element'; +import { normalizeSpacing, normalizeSpacerunSpans } from '@ckeditor/ckeditor5-paste-from-office/src/filters/space'; + +const BULLET_REGEXP = /(((.|\r?\n)*?)<\/span>)/g; + +export default class MathPasteFromOffice extends Plugin { + static get pluginName() { + return 'MathPasteFromOffice'; + } + + static get requires() { + return [ Clipboard ]; + } + + init() { + const editor = this.editor; + + editor.plugins.get( 'Clipboard' ).on( + 'inputTransformation', + ( evt, data ) => { + console.log( 'isTransformedWithPasteFromOffice', data.isTransformedWithPasteFromOffice ); // eslint-disable-line + console.log( data.content ); // eslint-disable-line + + if ( data.isTransformedWithMathPasteFromOffice ) { + return; + } + + if ( !data.isTransformedWithPasteFromOffice ) { + console.warn( 'Paste from office must be set before this' );// eslint-disable-line + return; + } + + const domParser = new DOMParser(); // eslint-disable-line + + /* + console.log(htmlString); // eslint-disable-line + const { body } = parseHtml( htmlString ); + console.log(body); // eslint-disable-line + */ + + const plainString = data.dataTransfer.getData( 'text/plain' ); + const lines = plainString.split( /\r?\n/ ); // split by new lines + if ( lines.length > 1 ) { + lines.pop(); // Remove last new line + } + // console.log(plainString); // eslint-disable-line + + const htmlMimeType = 'text/html'; + let htmlString = data.dataTransfer.getData( htmlMimeType ); + // console.log(htmlString); // eslint-disable-line + + htmlString = htmlString.replace( /