UNPKG

@ckeditor/ckeditor5-import-word

Version:

Import from Word feature for CKEditor 5.

23 lines 9.59 kB
/* * Copyright (c) 2003-2025, 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 _0x471fda,Command as _0xe23152}from'@ckeditor/ckeditor5-core/dist/index.js';import{IconImportWord as _0x12d10e}from'@ckeditor/ckeditor5-icons/dist/index.js';import{FileDialogButtonView as _0x5188c9,MenuBarMenuListItemFileDialogButtonView as _0x9a620c,SpinnerView as _0x4dce5f,Notification as _0x4cd83b}from'@ckeditor/ckeditor5-ui/dist/index.js';import{ClipboardPipeline as _0x1932ad}from'@ckeditor/ckeditor5-clipboard/dist/index.js';import{Range as _0x526375}from'@ckeditor/ckeditor5-engine/dist/index.js';import{first as _0x152a0f,uid as _0x2c1a5,logWarning as _0x4da11d,logError as _0x5922c9}from'@ckeditor/ckeditor5-utils/dist/index.js';function p(_0x85d968,_0x2b2ec6){const t=_0x85d968['t'];switch(_0x2b2ec6){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 _0x2b2ec6;}}class i extends _0x471fda{static get['pluginName'](){return'ImportWordUI';}static get['isOfficialPlugin'](){return!0x0;}static get['isPremiumPlugin'](){return!0x0;}['init'](){const _0x5ebbd0=this['editor'];_0x5ebbd0['ui']['componentFactory']['add']('importWord',()=>{const _0x4113c4=this['_createButton'](_0x5188c9),_0x30fc21=_0x5ebbd0['commands']['get']('importWord');return _0x4113c4['bind']('isOn')['to'](_0x30fc21,'isBusy'),_0x4113c4['tooltip']=!0x0,_0x4113c4;}),_0x5ebbd0['ui']['componentFactory']['add']('menuBar:importWord',()=>this['_createButton'](_0x9a620c));}['_createButton'](_0x237f8a){const _0x1e512c=this['editor'],_0x19ee7c=_0x1e512c['commands']['get']('importWord'),_0x1089c2=new _0x237f8a(_0x1e512c['locale']),_0x34d596=new _0x4dce5f();return _0x1089c2['set']({'acceptedType':'application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.wordprocessingml.template'}),_0x1089c2['set']({'icon':_0x12d10e}),_0x1089c2['bind']('isEnabled')['to'](_0x19ee7c),_0x1089c2['bind']('label')['to'](_0x19ee7c,'isBusy',_0x18abc2=>p(_0x1e512c['locale'],_0x18abc2?'Importing\x20Word\x20document':'Import\x20from\x20Word')),_0x34d596['bind']('isVisible')['to'](_0x19ee7c,'isBusy'),_0x1089c2['iconView']['bind']('isVisible')['to'](_0x19ee7c,'isBusy',_0x438480=>!_0x438480),_0x1089c2['children']['add'](_0x34d596),_0x1089c2['on']('done',(_0x4ddcc6,_0x1dc403)=>{_0x1e512c['execute']('importWord',_0x1dc403[0x0],{}),_0x1e512c['editing']['view']['focus']();}),_0x1089c2;}}class d extends _0xe23152{['_importMarker'];['_undoStepBatch'];['_abortController'];constructor(_0x425250){super(_0x425250),this['set']('isBusy',!0x1),this['_importMarker']=null,this['_undoStepBatch']=null,this['_abortController']=null,this['on']('dataInsert',(_0x740603,_0x22f77f)=>{const _0x15c6fb=new DataTransfer();_0x15c6fb['setData']('text/html',_0x22f77f['html']);const _0x9dfece=this['_importMarker']['getRange']();this['editor']['model']['enqueueChange'](this['_undoStepBatch'],_0x924219=>{this['_importMarker']['stopListening'](),_0x924219['setSelection'](_0x9dfece),_0x425250['editing']['view']['document']['fire']('clipboardInput',{'dataTransfer':_0x15c6fb});});},{'priority':'low'});}['refresh'](){this['isEnabled']=this['_checkEnabled']();}['execute'](_0x1e2e97,_0x1f1194={}){if(this['isBusy'])return Promise['resolve']();const _0x28096a=this['editor']['config']['get']('importWord');if(this['editor']['plugins']['has']('MergeFields')){const {prefix:_0x15f41b,suffix:_0xb31c28}=this['editor']['config']['get']('mergeFields');_0x1f1194['merge_fields']={'prefix':_0x15f41b,'suffix':_0xb31c28};}const _0x2573cb={'url':_0x28096a['converterUrl'],'file':_0x1e2e97,'serviceConfig':{..._0x1f1194,'formatting':_0x28096a['formatting'],'timezone':new Intl['DateTimeFormat']()['resolvedOptions']()['timeZone']}};return this['_prepareForImport'](),this['_sendImportRequest'](_0x2573cb)['then'](this['_handleImportResponse']['bind'](this))['catch'](this['_handleImportFailure']['bind'](this))['finally'](this['_cleanUpAfterImport']['bind'](this));}['_checkEnabled'](){if(this['isBusy'])return!0x1;const _0x4c6294=this['editor']['model'],_0x4c863f=_0x4c6294['schema'],_0x5a88f1=_0x4c6294['document']['selection'],_0x150b3a=_0x152a0f(_0x5a88f1['getSelectedBlocks']());return!!_0x150b3a&&_0x4c863f['checkChild'](_0x150b3a['parent'],'paragraph');}['_prepareForImport'](){const _0xbcab6d=this['editor'],_0x5dd30a=_0xbcab6d['model'],_0x33c148=_0x5dd30a['document']['selection'];this['_abortController']=new AbortController(),this['_undoStepBatch']=_0xbcab6d['model']['createBatch']({'isUndoable':!0x0}),_0x5dd30a['enqueueChange'](this['_undoStepBatch'],_0x2c8826=>{_0x33c148['isCollapsed']||_0x5dd30a['deleteContent'](_0x33c148,{'leaveUnmerged':!0x0});const _0xcc1484=_0x33c148['getFirstPosition'](),_0x2c1307=_0x152a0f(_0x33c148['getSelectedBlocks']());let _0x38aac9;_0x38aac9=_0x2c1307['is']('element','paragraph')&&_0x2c1307['isEmpty']?_0xcc1484:_0xcc1484['isAtStart']?_0x5dd30a['createPositionBefore'](_0x2c1307):_0xcc1484['isAtEnd']?_0x5dd30a['createPositionAfter'](_0x2c1307):_0x2c8826['split'](_0xcc1484)['position'],this['_importMarker']=_0x2c8826['addMarker']('importWord:'+_0x2c1a5(),{'usingOperation':!0x1,'affectsData':!0x1,'range':new _0x526375(_0x38aac9)}),this['_importMarker']['on']('change:range',()=>{'$graveyard'===this['_importMarker']['getRange']()['root']['rootName']&&this['_abortController']['abort']();});}),this['isBusy']=!0x0,this['refresh']();}['_cleanUpAfterImport'](){const _0x5c8a9b=this['editor']['model'];_0x5c8a9b['markers']['has'](this['_importMarker'])&&_0x5c8a9b['enqueueChange'](this['_undoStepBatch'],_0x429e6a=>{_0x429e6a['removeMarker'](this['_importMarker']);}),this['_importMarker']=null,this['_undoStepBatch']=null,this['_abortController']=null,this['isBusy']=!0x1,this['refresh']();}['_sendImportRequest']({url:_0x3a2f4b,file:_0x57bd1b,serviceConfig:_0x526352}){const _0x1066ad=this['editor']['plugins']['get']('ImportWordEditing')['getToken'](),_0x51f984=new FormData();_0x51f984['set']('config',JSON['stringify'](_0x526352)),_0x51f984['set']('file',_0x57bd1b);const _0x1f4505={'method':'POST','headers':{},'body':_0x51f984,'signal':this['_abortController']['signal']};return _0x1066ad&&(_0x1f4505['headers']['Authorization']=_0x1066ad['value']),fetch(_0x3a2f4b,_0x1f4505);}['_handleImportResponse'](_0x5cadbe){return _0x5cadbe['ok']?_0x5cadbe['json']()['then'](_0x4c1b88=>{if(!this['editor']['model']['markers']['has'](this['_importMarker']))return;'$graveyard'!==this['_importMarker']['getRange']()['root']['rootName']&&this['fire']('dataInsert',_0x4c1b88);}):Promise['reject']();}['_handleImportFailure'](){if(this['_abortController']['signal']['aborted'])return void _0x4da11d('import-word-plugin-import-cancelled');const _0x206030=this['editor'],_0x5cd79b=p(_0x206030['locale'],'An\x20error\x20occurred\x20while\x20importing\x20the\x20Word\x20file.');_0x206030['plugins']['get']('Notification')['showWarning'](_0x5cd79b),_0x5922c9('import-word-plugin-conversion-failed');}}class e extends _0x471fda{['_token'];static get['pluginName'](){return'ImportWordEditing';}static get['isOfficialPlugin'](){return!0x0;}static get['isPremiumPlugin'](){return!0x0;}static get['requires'](){return[_0x4cd83b,_0x1932ad,'CloudServices'];}constructor(_0x15366e){super(_0x15366e),this['_token']=null;}async['init'](){const _0x1125d1=this['editor'];_0x1125d1['config']['define']('importWord',{'converterUrl':'https://docx-converter.cke-cs.com/v2/convert/docx-html','tokenUrl':(_0x1125d1['config']['get']('cloudServices')||{})['tokenUrl'],'formatting':{'resets':'none','defaults':'none','styles':'inline'}}),_0x1125d1['commands']['add']('importWord',new d(_0x1125d1));const {tokenUrl:_0xd41dc9}=_0x1125d1['config']['get']('importWord');if(_0xd41dc9){const {tokenUrl:_0x2e59e5}=_0x1125d1['config']['get']('cloudServices')||{};this['_token']=_0xd41dc9===_0x2e59e5?_0x1125d1['plugins']['get']('CloudServices')['token']:await _0x1125d1['plugins']['get']('CloudServices')['registerTokenUrl'](_0xd41dc9);}}['getToken'](){return this['_token'];}}class o extends _0x471fda{static get['pluginName'](){return'ImportWord';}static get['isOfficialPlugin'](){return!0x0;}static get['isPremiumPlugin'](){return!0x0;}static get['requires'](){return[e,i];}}export{o as ImportWord,e as ImportWordEditing,i as ImportWordUI};