UNPKG

@ckeditor/ckeditor5-import-word

Version:

Import from Word feature for CKEditor 5.

23 lines 9.82 kB
/* * Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved. * * * * * +---------------------------------------------------------------------------------+ * | | * | Hello stranger! | * | | * | | * | What you're currently looking at is the source code of a legally protected, | * | proprietary software. Any attempts to deobfuscate / disassemble this code | * | are forbidden and will result in legal consequences. | * | | * | | * +---------------------------------------------------------------------------------+ * * * * */ import{Plugin as _0xbfe843,Command as _0x2dcd14}from'@ckeditor/ckeditor5-core/dist/index.js';import{IconImportWord as _0x26824e}from'@ckeditor/ckeditor5-icons/dist/index.js';import{FileDialogButtonView as _0xa36343,MenuBarMenuListItemFileDialogButtonView as _0x138fca,SpinnerView as _0x3f7787,Notification as _0x57392}from'@ckeditor/ckeditor5-ui/dist/index.js';import{ClipboardPipeline as _0x1ad04a}from'@ckeditor/ckeditor5-clipboard/dist/index.js';import{CloudServices as _0x5b4b89}from'@ckeditor/ckeditor5-cloud-services/dist/index.js';import{ModelRange as _0x227a66}from'@ckeditor/ckeditor5-engine/dist/index.js';import{first as _0x27c539,uid as _0x2b7e23,logWarning as _0x4296a5,logError as _0x65aabd}from'@ckeditor/ckeditor5-utils/dist/index.js';function f(_0x46d78c,_0x3c1bda){const t=_0x46d78c['t'];switch(_0x3c1bda){case'Import\x20from\x20Word':return t('Import\x20from\x20Word');case'An\x20error\x20occurred\x20while\x20importing\x20the\x20Word\x20file.':return t('An\x20error\x20occurred\x20while\x20importing\x20the\x20Word\x20file.');case'Importing\x20Word\x20document':return t('Importing\x20Word\x20document');default:return _0x3c1bda;}}class ImportWordUI extends _0xbfe843{static get['pluginName'](){return'ImportWordUI';}static get['isOfficialPlugin'](){return!0x0;}static get['isPremiumPlugin'](){return!0x0;}['init'](){const _0x23e9d1=this['editor'];_0x23e9d1['ui']['componentFactory']['add']('importWord',()=>{const _0x42b022=this['_createButton'](_0xa36343),_0x4f8603=_0x23e9d1['commands']['get']('importWord');return _0x42b022['bind']('isOn')['to'](_0x4f8603,'isBusy'),_0x42b022['tooltip']=!0x0,_0x42b022;}),_0x23e9d1['ui']['componentFactory']['add']('menuBar:importWord',()=>this['_createButton'](_0x138fca));}['_createButton'](_0x1bdf6f){const _0x41ce80=this['editor'],_0x45d7d1=_0x41ce80['commands']['get']('importWord'),_0xce4ee3=new _0x1bdf6f(_0x41ce80['locale']),_0x11542e=new _0x3f7787();return _0xce4ee3['set']({'acceptedType':'application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.wordprocessingml.template'}),_0xce4ee3['set']({'icon':_0x26824e}),_0xce4ee3['bind']('isEnabled')['to'](_0x45d7d1),_0xce4ee3['bind']('label')['to'](_0x45d7d1,'isBusy',_0x3da4e5=>f(_0x41ce80['locale'],_0x3da4e5?'Importing\x20Word\x20document':'Import\x20from\x20Word')),_0x11542e['bind']('isVisible')['to'](_0x45d7d1,'isBusy'),_0xce4ee3['iconView']['bind']('isVisible')['to'](_0x45d7d1,'isBusy',_0x5118da=>!_0x5118da),_0xce4ee3['children']['add'](_0x11542e),_0xce4ee3['on']('done',(_0x4506ce,_0x5d7cb5)=>{_0x41ce80['execute']('importWord',_0x5d7cb5[0x0],{}),_0x41ce80['editing']['view']['focus']();}),_0xce4ee3;}}class ImportWordCommand extends _0x2dcd14{['_importMarker'];['_undoStepBatch'];['_abortController'];constructor(_0x3b43a0){super(_0x3b43a0),this['set']('isBusy',!0x1),this['_importMarker']=null,this['_undoStepBatch']=null,this['_abortController']=null,this['on']('dataInsert',(_0x5f5ce4,_0x11c46b)=>{const _0x916add=new DataTransfer();_0x916add['setData']('text/html',_0x11c46b['html']);const _0x1645c8=this['_importMarker']['getRange']();this['editor']['model']['enqueueChange'](this['_undoStepBatch'],_0xd69c4c=>{this['_importMarker']['stopListening'](),_0xd69c4c['setSelection'](_0x1645c8),_0x3b43a0['editing']['view']['document']['fire']('clipboardInput',{'dataTransfer':_0x916add});});},{'priority':'low'});}['refresh'](){this['isEnabled']=this['_checkEnabled']();}['execute'](_0x4e9cce,_0x2537ab={}){if(this['isBusy'])return Promise['resolve']();const _0x23822d=this['editor']['config']['get']('importWord');if(this['editor']['plugins']['has']('MergeFields')){const {prefix:_0x127e46,suffix:_0x48281e}=this['editor']['config']['get']('mergeFields');_0x2537ab['merge_fields']={'prefix':_0x127e46,'suffix':_0x48281e};}const _0x4b081b={'url':_0x23822d['converterUrl'],'file':_0x4e9cce,'serviceConfig':{..._0x2537ab,'formatting':_0x23822d['formatting'],'timezone':new Intl['DateTimeFormat']()['resolvedOptions']()['timeZone']}};return this['_prepareForImport'](),this['_sendImportRequest'](_0x4b081b)['then'](this['_handleImportResponse']['bind'](this))['catch'](this['_handleImportFailure']['bind'](this))['finally'](this['_cleanUpAfterImport']['bind'](this));}['_checkEnabled'](){if(this['isBusy'])return!0x1;const _0x28cf3a=this['editor']['model'],_0x4b5ae1=_0x28cf3a['schema'],_0x4aac7b=_0x28cf3a['document']['selection'],_0x58a86f=_0x27c539(_0x4aac7b['getSelectedBlocks']());return!!_0x58a86f&&_0x4b5ae1['checkChild'](_0x58a86f['parent'],'paragraph');}['_prepareForImport'](){const _0x2c0e41=this['editor'],_0x579d79=_0x2c0e41['model'],_0x48501e=_0x579d79['document']['selection'];this['_abortController']=new AbortController(),this['_undoStepBatch']=_0x2c0e41['model']['createBatch']({'isUndoable':!0x0}),_0x579d79['enqueueChange'](this['_undoStepBatch'],_0x1f5dc4=>{_0x48501e['isCollapsed']||_0x579d79['deleteContent'](_0x48501e,{'leaveUnmerged':!0x0});const _0x5ec163=_0x48501e['getFirstPosition'](),_0x3f46cc=_0x27c539(_0x48501e['getSelectedBlocks']());let _0x34e1b1;_0x34e1b1=_0x3f46cc['is']('element','paragraph')&&_0x3f46cc['isEmpty']?_0x5ec163:_0x5ec163['isAtStart']?_0x579d79['createPositionBefore'](_0x3f46cc):_0x5ec163['isAtEnd']?_0x579d79['createPositionAfter'](_0x3f46cc):_0x1f5dc4['split'](_0x5ec163)['position'],this['_importMarker']=_0x1f5dc4['addMarker']('importWord:'+_0x2b7e23(),{'usingOperation':!0x1,'affectsData':!0x1,'range':new _0x227a66(_0x34e1b1)}),this['_importMarker']['on']('change:range',()=>{'$graveyard'===this['_importMarker']['getRange']()['root']['rootName']&&this['_abortController']['abort']();});}),this['isBusy']=!0x0,this['refresh']();}['_cleanUpAfterImport'](){const _0xd14732=this['editor']['model'];_0xd14732['markers']['has'](this['_importMarker'])&&_0xd14732['enqueueChange'](this['_undoStepBatch'],_0x28edf1=>{_0x28edf1['removeMarker'](this['_importMarker']);}),this['_importMarker']=null,this['_undoStepBatch']=null,this['_abortController']=null,this['isBusy']=!0x1,this['refresh']();}async['_sendImportRequest']({url:_0x214812,file:_0x557dbf,serviceConfig:_0x50bb85}){const _0x512f59=this['editor'],_0xdce1bc=await _0x512f59['plugins']['get']('ImportWordEditing')['getToken'](),_0x13fad0=new FormData();_0x13fad0['set']('config',JSON['stringify'](_0x50bb85)),_0x13fad0['set']('file',_0x557dbf);const _0x50c08b={'method':'POST','headers':{},'body':_0x13fad0,'signal':this['_abortController']['signal']};return _0xdce1bc&&(_0x50c08b['headers']['Authorization']=_0xdce1bc['value']),fetch(_0x214812,_0x50c08b);}['_handleImportResponse'](_0x9eac6e){return _0x9eac6e['ok']?_0x9eac6e['json']()['then'](_0x30c877=>{if(!this['editor']['model']['markers']['has'](this['_importMarker']))return;'$graveyard'!==this['_importMarker']['getRange']()['root']['rootName']&&this['fire']('dataInsert',{..._0x30c877,'undoStepBatch':this['_undoStepBatch']});}):Promise['reject']();}['_handleImportFailure'](){if(this['_abortController']['signal']['aborted'])return void _0x4296a5('import-word-plugin-import-cancelled');const _0x54a452=this['editor'],_0x219b4d=f(_0x54a452['locale'],'An\x20error\x20occurred\x20while\x20importing\x20the\x20Word\x20file.');_0x54a452['plugins']['get']('Notification')['showWarning'](_0x219b4d),_0x65aabd('import-word-plugin-conversion-failed');}}class ImportWordEditing extends _0xbfe843{['_token'];static get['pluginName'](){return'ImportWordEditing';}static get['isOfficialPlugin'](){return!0x0;}static get['isPremiumPlugin'](){return!0x0;}static get['requires'](){return[_0x57392,_0x1ad04a,_0x5b4b89];}['init'](){const _0x398f11=this['editor'],_0x1b7f01=_0x398f11['plugins']['get'](_0x5b4b89);_0x398f11['config']['define']('importWord',{'converterUrl':'https://docx-converter.cke-cs.com/v2/convert/docx-html','tokenUrl':(_0x398f11['config']['get']('cloudServices')||{})['tokenUrl'],'formatting':{'resets':'none','defaults':'none','styles':'inline'}}),_0x398f11['commands']['add']('importWord',new ImportWordCommand(_0x398f11));const {tokenUrl:_0x1c9951}=_0x398f11['config']['get']('importWord');if(_0x1c9951){const {tokenUrl:_0xaa48fe}=_0x398f11['config']['get']('cloudServices')||{};this['_token']=_0x1c9951===_0xaa48fe?Promise['resolve'](_0x1b7f01['token']):_0x1b7f01['registerTokenUrl'](_0x1c9951);}else this['_token']=Promise['resolve'](null);}['getToken'](){return this['_token'];}}class ImportWord extends _0xbfe843{static get['pluginName'](){return'ImportWord';}static get['isOfficialPlugin'](){return!0x0;}static get['isPremiumPlugin'](){return!0x0;}static get['requires'](){return[ImportWordEditing,ImportWordUI];}}export{ImportWord,ImportWordCommand,ImportWordEditing,ImportWordUI};