UNPKG

@ckeditor/ckeditor5-import-word

Version:

Import from Word feature for CKEditor 5.

23 lines 9.62 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 _0x184a31,Command as _0x3bcd8d}from'@ckeditor/ckeditor5-core/dist/index.js';import{IconImportWord as _0x22a2f8}from'@ckeditor/ckeditor5-icons/dist/index.js';import{FileDialogButtonView as _0x263447,MenuBarMenuListItemFileDialogButtonView as _0x5d495c,SpinnerView as _0x1e0817,Notification as _0x422a1a}from'@ckeditor/ckeditor5-ui/dist/index.js';import{ClipboardPipeline as _0x56ad0a}from'@ckeditor/ckeditor5-clipboard/dist/index.js';import{ModelRange as _0x7fb78e}from'@ckeditor/ckeditor5-engine/dist/index.js';import{first as _0x2511e5,uid as _0x383ab5,logWarning as _0x220ec0,logError as _0x2ea0f6}from'@ckeditor/ckeditor5-utils/dist/index.js';function c(_0xb8b6d9,_0x1fbf1f){const t=_0xb8b6d9['t'];switch(_0x1fbf1f){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 _0x1fbf1f;}}class i extends _0x184a31{static get['pluginName'](){return'ImportWordUI';}static get['isOfficialPlugin'](){return!0x0;}static get['isPremiumPlugin'](){return!0x0;}['init'](){const _0x16640e=this['editor'];_0x16640e['ui']['componentFactory']['add']('importWord',()=>{const _0x528dd5=this['_createButton'](_0x263447),_0x2c8ea6=_0x16640e['commands']['get']('importWord');return _0x528dd5['bind']('isOn')['to'](_0x2c8ea6,'isBusy'),_0x528dd5['tooltip']=!0x0,_0x528dd5;}),_0x16640e['ui']['componentFactory']['add']('menuBar:importWord',()=>this['_createButton'](_0x5d495c));}['_createButton'](_0x1f2bf9){const _0xc64a62=this['editor'],_0x49b5f2=_0xc64a62['commands']['get']('importWord'),_0x2f625e=new _0x1f2bf9(_0xc64a62['locale']),_0x29a3f3=new _0x1e0817();return _0x2f625e['set']({'acceptedType':'application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.wordprocessingml.template'}),_0x2f625e['set']({'icon':_0x22a2f8}),_0x2f625e['bind']('isEnabled')['to'](_0x49b5f2),_0x2f625e['bind']('label')['to'](_0x49b5f2,'isBusy',_0x182f12=>c(_0xc64a62['locale'],_0x182f12?'Importing\x20Word\x20document':'Import\x20from\x20Word')),_0x29a3f3['bind']('isVisible')['to'](_0x49b5f2,'isBusy'),_0x2f625e['iconView']['bind']('isVisible')['to'](_0x49b5f2,'isBusy',_0x4eb99a=>!_0x4eb99a),_0x2f625e['children']['add'](_0x29a3f3),_0x2f625e['on']('done',(_0x27a0d4,_0xe2c53e)=>{_0xc64a62['execute']('importWord',_0xe2c53e[0x0],{}),_0xc64a62['editing']['view']['focus']();}),_0x2f625e;}}class u extends _0x3bcd8d{['_importMarker'];['_undoStepBatch'];['_abortController'];constructor(_0x1868a9){super(_0x1868a9),this['set']('isBusy',!0x1),this['_importMarker']=null,this['_undoStepBatch']=null,this['_abortController']=null,this['on']('dataInsert',(_0x4ebafe,_0x480f45)=>{const _0x4194eb=new DataTransfer();_0x4194eb['setData']('text/html',_0x480f45['html']);const _0x4ccb22=this['_importMarker']['getRange']();this['editor']['model']['enqueueChange'](this['_undoStepBatch'],_0x2069a7=>{this['_importMarker']['stopListening'](),_0x2069a7['setSelection'](_0x4ccb22),_0x1868a9['editing']['view']['document']['fire']('clipboardInput',{'dataTransfer':_0x4194eb});});},{'priority':'low'});}['refresh'](){this['isEnabled']=this['_checkEnabled']();}['execute'](_0x3e834e,_0x8f2dad={}){if(this['isBusy'])return Promise['resolve']();const _0x3b1603=this['editor']['config']['get']('importWord');if(this['editor']['plugins']['has']('MergeFields')){const {prefix:_0x40812b,suffix:_0x227e32}=this['editor']['config']['get']('mergeFields');_0x8f2dad['merge_fields']={'prefix':_0x40812b,'suffix':_0x227e32};}const _0x476cd3={'url':_0x3b1603['converterUrl'],'file':_0x3e834e,'serviceConfig':{..._0x8f2dad,'formatting':_0x3b1603['formatting'],'timezone':new Intl['DateTimeFormat']()['resolvedOptions']()['timeZone']}};return this['_prepareForImport'](),this['_sendImportRequest'](_0x476cd3)['then'](this['_handleImportResponse']['bind'](this))['catch'](this['_handleImportFailure']['bind'](this))['finally'](this['_cleanUpAfterImport']['bind'](this));}['_checkEnabled'](){if(this['isBusy'])return!0x1;const _0x55c151=this['editor']['model'],_0x3489bd=_0x55c151['schema'],_0x548128=_0x55c151['document']['selection'],_0x537c5f=_0x2511e5(_0x548128['getSelectedBlocks']());return!!_0x537c5f&&_0x3489bd['checkChild'](_0x537c5f['parent'],'paragraph');}['_prepareForImport'](){const _0x53408f=this['editor'],_0x3084ce=_0x53408f['model'],_0x40b9ce=_0x3084ce['document']['selection'];this['_abortController']=new AbortController(),this['_undoStepBatch']=_0x53408f['model']['createBatch']({'isUndoable':!0x0}),_0x3084ce['enqueueChange'](this['_undoStepBatch'],_0x3abdca=>{_0x40b9ce['isCollapsed']||_0x3084ce['deleteContent'](_0x40b9ce,{'leaveUnmerged':!0x0});const _0x1de620=_0x40b9ce['getFirstPosition'](),_0x32c28c=_0x2511e5(_0x40b9ce['getSelectedBlocks']());let _0x3939f5;_0x3939f5=_0x32c28c['is']('element','paragraph')&&_0x32c28c['isEmpty']?_0x1de620:_0x1de620['isAtStart']?_0x3084ce['createPositionBefore'](_0x32c28c):_0x1de620['isAtEnd']?_0x3084ce['createPositionAfter'](_0x32c28c):_0x3abdca['split'](_0x1de620)['position'],this['_importMarker']=_0x3abdca['addMarker']('importWord:'+_0x383ab5(),{'usingOperation':!0x1,'affectsData':!0x1,'range':new _0x7fb78e(_0x3939f5)}),this['_importMarker']['on']('change:range',()=>{'$graveyard'===this['_importMarker']['getRange']()['root']['rootName']&&this['_abortController']['abort']();});}),this['isBusy']=!0x0,this['refresh']();}['_cleanUpAfterImport'](){const _0xefb2c2=this['editor']['model'];_0xefb2c2['markers']['has'](this['_importMarker'])&&_0xefb2c2['enqueueChange'](this['_undoStepBatch'],_0x7f08d7=>{_0x7f08d7['removeMarker'](this['_importMarker']);}),this['_importMarker']=null,this['_undoStepBatch']=null,this['_abortController']=null,this['isBusy']=!0x1,this['refresh']();}['_sendImportRequest']({url:_0x1a2303,file:_0x2a077d,serviceConfig:_0x364c60}){const _0xad462=this['editor']['plugins']['get']('ImportWordEditing')['getToken'](),_0xe6ee26=new FormData();_0xe6ee26['set']('config',JSON['stringify'](_0x364c60)),_0xe6ee26['set']('file',_0x2a077d);const _0x347903={'method':'POST','headers':{},'body':_0xe6ee26,'signal':this['_abortController']['signal']};return _0xad462&&(_0x347903['headers']['Authorization']=_0xad462['value']),fetch(_0x1a2303,_0x347903);}['_handleImportResponse'](_0x54451f){return _0x54451f['ok']?_0x54451f['json']()['then'](_0x38fdb3=>{if(!this['editor']['model']['markers']['has'](this['_importMarker']))return;'$graveyard'!==this['_importMarker']['getRange']()['root']['rootName']&&this['fire']('dataInsert',_0x38fdb3);}):Promise['reject']();}['_handleImportFailure'](){if(this['_abortController']['signal']['aborted'])return void _0x220ec0('import-word-plugin-import-cancelled');const _0x48a8a4=this['editor'],_0x5e37bb=c(_0x48a8a4['locale'],'An\x20error\x20occurred\x20while\x20importing\x20the\x20Word\x20file.');_0x48a8a4['plugins']['get']('Notification')['showWarning'](_0x5e37bb),_0x2ea0f6('import-word-plugin-conversion-failed');}}class e extends _0x184a31{['_token'];static get['pluginName'](){return'ImportWordEditing';}static get['isOfficialPlugin'](){return!0x0;}static get['isPremiumPlugin'](){return!0x0;}static get['requires'](){return[_0x422a1a,_0x56ad0a,'CloudServices'];}constructor(_0x205f79){super(_0x205f79),this['_token']=null;}async['init'](){const _0x3380cd=this['editor'];_0x3380cd['config']['define']('importWord',{'converterUrl':'https://docx-converter.cke-cs.com/v2/convert/docx-html','tokenUrl':(_0x3380cd['config']['get']('cloudServices')||{})['tokenUrl'],'formatting':{'resets':'none','defaults':'none','styles':'inline'}}),_0x3380cd['commands']['add']('importWord',new u(_0x3380cd));const {tokenUrl:_0x339fe0}=_0x3380cd['config']['get']('importWord');if(_0x339fe0){const {tokenUrl:_0x1e30e7}=_0x3380cd['config']['get']('cloudServices')||{};this['_token']=_0x339fe0===_0x1e30e7?_0x3380cd['plugins']['get']('CloudServices')['token']:await _0x3380cd['plugins']['get']('CloudServices')['registerTokenUrl'](_0x339fe0);}}['getToken'](){return this['_token'];}}class x extends _0x184a31{static get['pluginName'](){return'ImportWord';}static get['isOfficialPlugin'](){return!0x0;}static get['isPremiumPlugin'](){return!0x0;}static get['requires'](){return[e,i];}}export{x as ImportWord,u as ImportWordCommand,e as ImportWordEditing,i as ImportWordUI};