From 64527c5e0842d597cd3d5c2f0905dc1dca2067d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20H=C3=B6rrmann?= Date: Tue, 24 Jan 2023 12:54:11 +0100 Subject: [PATCH] Fix image uploads Closes #159 --- composer.json | 2 +- index.js | 2 +- src/components/MarkdownInput.vue | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 482aa19..ecb6b32 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "fabianmichael/kirby-markdown-field", "description": "Super-sophisticated markdown editor for Kirby 3", "type": "kirby-plugin", - "version": "2.1.3", + "version": "2.1.4", "license": "MIT", "authors": [ { diff --git a/index.js b/index.js index a2bfe50..77e56f2 100644 --- a/index.js +++ b/index.js @@ -42,4 +42,4 @@ __p+='`),h}),s+=`'; `+s,e.dispatch({changes:{from:0,to:t.doc.length,insert:r+s},selection:b.cursor(r.length),scrollIntoView:!0})}}configure(e){if(typeof e=="string"&&(e={mark:e}),N.prototype.configure.call(this,e),!["***","---","___"].includes(this.options.mark))throw"Horizontal rule mark must be either `***`, `---` or `___`."}get defaults(){return{mark:"***"}}get name(){return"hr"}get syntax(){return{token:this.token,type:this.tokenType,render:()=>this.options.mark}}get token(){return"HorizontalRule"}get tokenType(){return"block"}get isActive(){return()=>!1}get isDisabled(){return()=>this.editor.isActiveToken("Kirbytag","Link")}}class ry extends N{get button(){return{icon:"code",label:this.input.$t("toolbar.button.code")+this.formatKeyName(this.keys()[0]),command:this.command}}get command(){return()=>!this.isDisabled()&&this.editor.toggleInlineFormat(this.token)}keys(){return[{mac:"Ctrl-Alt-x",key:"Alt-Shift-x",run:this.command,preventDefault:!0}]}get name(){return"code"}get token(){return"InlineCode"}get tokenType(){return"inline"}}class sy extends N{get button(){return{align:"right",icon:"preview",label:this.input.$t("markdown.toolbar.button.invisibles")+this.formatKeyName(this.keys()[0]),command:this.command}}get command(){return()=>this.editor.toggleInvisibles()}keys(){return[{mac:"Ctrl-Alt-i",key:"Alt-Shift-i",run:this.command,preventDefault:!0}]}get name(){return"invisibles"}get tokenType(){return"setting"}get isDisabled(){return()=>!1}}class oy extends N{get button(){return{icon:"url",label:this.input.$t("toolbar.button.link")+this.formatKeyName(this.keys()[0]),command:()=>this.editor.emit("dialog",this)}}get defaults(){return{blank:!0}}get command(){return e=>{if(!this.isDisabled())if(e.type==="email"){const t=e.email!==null?e.email:"";if(this.input.kirbytext){const n=e.text?` text: ${e.text}`:"";this.editor.insert(`(email: ${t}${n})`)}else e.text?this.editor.insert(`[${e.text}](mailto:${t})`):this.editor.insert(`<${t}>`)}else{const t=e.url!==null?e.url:"";if(this.input.kirbytext){const n=e.text?` text: ${e.text}`:"",r=e.blank?" target: _blank":"";this.editor.insert(`(link: ${t}${n}${r})`)}else e.text?this.editor.insert(`[${e.text}](${t})`):this.editor.insert(`<${t}>`)}}}get dialog(){return"k-markdown-link-dialog"}keys(){return[{key:"Mod-k",run:()=>this.editor.emit("dialog",this)}]}get name(){return"link"}}class ly extends N{get button(){return{icon:"list-numbers",label:this.input.$t("toolbar.button.ol")+this.formatKeyName(this.keys()[0]),command:this.command}}get command(){return()=>this.editor.toggleBlockFormat(this.token)}keys(){return[{mac:"Ctrl-Alt-o",key:"Alt-Shift-o",run:this.command,preventDefault:!0}]}get name(){return"ol"}get token(){return"OrderedList"}get tokenType(){return"block"}}class ay extends N{get button(){return{icon:"pagelink",label:this.input.$t("markdown.toolbar.button.pagelink"),command:()=>{this.editor.emit("dialog",this,{endpoint:this.input.endpoints.field+"/pages",multiple:!1,selected:[]})}}}get command(){return e=>{if(this.isDisabled()||!e||!e.length)return;const t=e[0],n=this.editor.getSelection(),r=n.length>0?n:t.text||t.title,s=this.input.currentLanguage&&!this.input.currentLanguage.default?` lang: ${this.input.currentLanguage.code}`:"",o=`(link: ${t.id} text: ${r}${s})`;this.editor.insert(o)}}get dialog(){return"k-pages-dialog"}get name(){return"pagelink"}}class hy extends N{get button(){return{icon:"special-chars",label:this.input.$t("toolbar.button.headings"),dropdown:[{label:"No-Break Space"+this.formatKeyName({mac:"Alt-Space"},"",""),command:()=>this.editor.insert("\xA0")},{label:"Thin Space",command:()=>this.editor.insert("\u2009")},{label:"Thin No-Break Space",command:()=>this.editor.insert("\u202F")},{label:"Soft Hyphen",command:()=>this.editor.insert("\xAD")},{label:"Zero-Width Space",command:()=>this.editor.insert("\u200B")}]}}get isDisabled(){return()=>!1}get name(){return"chars"}}class uy extends N{get button(){return{icon:"strikethrough",label:this.input.$t("markdown.toolbar.button.strikethrough")+this.formatKeyName(this.keys()[0]),command:this.command}}get command(){return()=>!this.isDisabled()&&this.editor.toggleInlineFormat(this.token)}keys(){return[{mac:"Ctrl-Alt-d",key:"Alt-Shift-d",run:this.command,preventDefault:!0}]}get name(){return"strikethrough"}get token(){return"Strikethrough"}get tokenType(){return"inline"}}class cy extends N{get button(){return{icon:"bold",label:this.input.$t("toolbar.button.bold")+this.formatKeyName(this.keys()[0]),command:this.command}}get command(){return()=>!this.isDisabled()&&this.editor.toggleInlineFormat(this.token)}configure(e){if(typeof e=="string"&&(e={mark:e}),N.prototype.configure.call(this,e),!["**","__"].includes(this.options.mark))throw"Bold mark must be either `**` or `__`."}get defaults(){return{mark:"**"}}keys(){return[{key:"Mod-b",run:this.command,preventDefault:!0}]}get name(){return"bold"}get syntax(){return{token:this.token,type:this.tokenType,mark:this.options.mark}}get token(){return"StrongEmphasis"}get tokenType(){return"inline"}}const Ty="",fy={components:{"k-markdown-toolbar":pd,"k-markdown-link-dialog":xd},data(){return{editor:Object,skipNextChangeEvent:!1,currentDialog:null,activeMarks:[],isDragOver:!1,invisibles:!1,toolbarButtons:[],active:[],dialogs:[]}},props:{...Bo.props,hideToolbar:{type:Boolean,default:!1},forceLayout:{type:[String,Boolean],default:!1},fontSize:{type:String,default:"normal"}},computed:{currentLanguage(){return this.$language}},watch:{value(i,e){i!==void 0&&i!==this.editor.value&&this.editor.setValue(i)}},mounted(){this.editor=new Gk(this.value,{readOnly:this.disabled,element:this.$refs.input,input:this,placeholder:this.placeholder,invisibles:this.invisibles,spellcheck:this.spellcheck,extensions:[...this.createHighlights(),...this.createToolbarButtons(),...this.createCustomExtensions()],events:{active:i=>{this.active=i},dialog:(i,...e)=>{this.openDialog(i,...e)},update:i=>{this.$refs.toolbar&&this.$refs.toolbar.closeDropdowns(),this.$emit("input",i)},invisibles:i=>{this.invisibles=i}}}),this.toolbarButtons=this.editor.buttons,this.dialogs=this.editor.dialogs,this.autofocus&&!this.disabled&&this.focus().then(()=>{this.editor.view.dispatch({scrollIntoView:!0,selection:{head:this.editor.state.doc.length,anchor:this.editor.state.doc.length}})})},beforeDestroy(){this.editor.destroy()},methods:{focus(){this.editor.focus()},onSubmit(i){return this.$emit("submit",i)},createButtons(){return window.markdownEditorButtons?window.markdownEditorButtons.reduce((i,e)=>[...i,N.factory(e)],[]):[]},createCustomExtensions(){return window.markdownEditorExtensions?window.markdownEditorExtensions.reduce((i,e)=>[...i,ve.factory(e)],[]):[]},createToolbarButtons(){const i=[new jk,new Fk,new Hk,new Kk,new Jk,new ey,new ty,new iy,new ny,new ry,new sy,new oy,new ly,new hy,new uy,new cy,...this.createButtons()];this.kirbytext&&i.push(new ay);const e=i.reduce((n,r)=>({...n,[r.name]:r}),{});this.buttons===!0&&(this.buttons={headlines:["h1","h2","h3"],bold:{},italic:{},divider__0:{},link:{},file:{},image:{},code:{},divider__1:{},ul:{},ol:{},invisibles:{}});const t=[];for(let n of Object.keys(this.buttons))n=n.replace(/__.*$/,""),e[n]&&(e[n].configure(this.buttons[n]),t.push(e[n]));return t},createHighlights(){return this.highlights===!1?[]:this.customHighlights.filter(e=>this.highlights===!0||Array.isArray(this.highlights)&&this.highlights.includes(e.name)).map(e=>new Yk(e))},openDialog(i,...e){const t=`dialog-${i.name}`,n=this.$refs[t][0];n.open(...e),this.currentDialog=n},cancel(){this.currentDialog=null,setTimeout(()=>this.focus())},submitDialog(i,...e){this.currentDialog=null,this.focus(),i.command(...e)},insertUpload(i,e){this.editor.insert(e.map(t=>t.dragText).join(` -`)),this.$events.$emit("file.create"),this.$events.$emit("model.update"),this.$store.dispatch("notification/success",":)")},selectFile(){this.$refs.fileDialog.open({endpoint:this.endpoints.field+"/files",multiple:!1})},uploadFile(){this.$refs.fileUpload.open({url:"/api/"+this.endpoints.field+"/upload",multiple:!1})},onDrop(i){if(this.isDragOver=!1,this.uploads&&this.$helper.isUploadEvent(i))return this.$refs.fileUpload.drop(i.dataTransfer.files,{url:"/api/"+this.endpoints.field+"/upload",multiple:!1});const e=this.$store.state.drag;e&&e.type==="text"&&(this.editor.insert(e.data,!0),this.focus())},onDragLeave(){this.$refs.input.blur(),this.isDragOver=!1},onDragOver(){this.isDragOver=!0},onDragEnter(i){if(this.uploads&&this.$helper.isUploadEvent(i)){i.dataTransfer.dropEffect="copy",this.focus(),this.isDragOver=!0;return}const e=this.$store.state.drag;e&&e.type==="text"&&(i.dataTransfer.dropEffect="copy",this.focus(),this.isDragOver=!0)}}};var dy=function(){var e=this,t=e._self._c;return t("div",{staticClass:"k-markdown-input-wrap",attrs:{"data-theme":e.theme,"data-font-family":e.font,"data-font-size":e.fontSize,"data-layout":e.layout,"data-dragover":!!e.isDragOver,"data-size":e.size,"data-invisibles":e.invisibles}},[e.buttons&&!e.disabled&&!e.hideToolbar?t("k-markdown-toolbar",{ref:"toolbar",attrs:{buttons:e.toolbarButtons,active:e.active,invisibles:e.invisibles},nativeOn:{mousedown:function(n){n.preventDefault()}}}):e._e(),t("div",{ref:"input",staticClass:"k-markdown-input",on:{dragenter:e.onDragEnter,dragover:e.onDragOver,dragleave:e.onDragLeave,drop:e.onDrop,keydown:[function(n){return!n.type.indexOf("key")&&e._k(n.keyCode,"enter",13,n.key,"Enter")||!n.metaKey?null:e.onSubmit.apply(null,arguments)},function(n){return!n.type.indexOf("key")&&e._k(n.keyCode,"enter",13,n.key,"Enter")||!n.ctrlKey?null:e.onSubmit.apply(null,arguments)}]}}),e._l(this.dialogs,function(n){return t(n.dialog,e._b({key:n.name,ref:`dialog-${n.name}`,refInFor:!0,tag:"component",attrs:{extension:n},on:{cancel:e.cancel,close:e.cancel,submit:function(r){return e.submitDialog(n,...arguments)}}},"component",e.$props,!1))}),e.uploads?t("k-upload",{ref:"fileUpload",on:{success:e.insertUpload}}):e._e()],2)},Oy=[],py=di(fy,dy,Oy,!1,null,null,null,null);const gy=py.exports;window.panel.plugin("fabianmichael/markdown-field",{components:{"k-markdown-input":gy},blocks:{markdown:od},fields:{markdown:Bo},icons:{"horizontal-rule":'',"special-chars":'',pagelink:'',h1:'',h2:'',h3:'',h4:'',h5:'',h6:'',footnote:'',highlight:'',eraser:''}})})(); +`)),this.$events.$emit("file.create"),this.$events.$emit("model.update"),this.$store.dispatch("notification/success",":)")},selectFile(){this.$refs.fileDialog.open({endpoint:this.endpoints.field+"/files",multiple:!1})},uploadFile(){this.$refs.fileUpload.open({url:this.$urls.api+"/"+this.endpoints.field+"/upload",multiple:!1})},onDrop(i){if(this.isDragOver=!1,this.uploads&&this.$helper.isUploadEvent(i))return this.$refs.fileUpload.drop(i.dataTransfer.files,{url:"/api/"+this.endpoints.field+"/upload",multiple:!1});const e=this.$store.state.drag;e&&e.type==="text"&&(this.editor.insert(e.data,!0),this.focus())},onDragLeave(){this.$refs.input.blur(),this.isDragOver=!1},onDragOver(){this.isDragOver=!0},onDragEnter(i){if(this.uploads&&this.$helper.isUploadEvent(i)){i.dataTransfer.dropEffect="copy",this.focus(),this.isDragOver=!0;return}const e=this.$store.state.drag;e&&e.type==="text"&&(i.dataTransfer.dropEffect="copy",this.focus(),this.isDragOver=!0)}}};var dy=function(){var e=this,t=e._self._c;return t("div",{staticClass:"k-markdown-input-wrap",attrs:{"data-theme":e.theme,"data-font-family":e.font,"data-font-size":e.fontSize,"data-layout":e.layout,"data-dragover":!!e.isDragOver,"data-size":e.size,"data-invisibles":e.invisibles}},[e.buttons&&!e.disabled&&!e.hideToolbar?t("k-markdown-toolbar",{ref:"toolbar",attrs:{buttons:e.toolbarButtons,active:e.active,invisibles:e.invisibles},nativeOn:{mousedown:function(n){n.preventDefault()}}}):e._e(),t("div",{ref:"input",staticClass:"k-markdown-input",on:{dragenter:e.onDragEnter,dragover:e.onDragOver,dragleave:e.onDragLeave,drop:e.onDrop,keydown:[function(n){return!n.type.indexOf("key")&&e._k(n.keyCode,"enter",13,n.key,"Enter")||!n.metaKey?null:e.onSubmit.apply(null,arguments)},function(n){return!n.type.indexOf("key")&&e._k(n.keyCode,"enter",13,n.key,"Enter")||!n.ctrlKey?null:e.onSubmit.apply(null,arguments)}]}}),e._l(this.dialogs,function(n){return t(n.dialog,e._b({key:n.name,ref:`dialog-${n.name}`,refInFor:!0,tag:"component",attrs:{extension:n},on:{cancel:e.cancel,close:e.cancel,submit:function(r){return e.submitDialog(n,...arguments)}}},"component",e.$props,!1))}),e.uploads?t("k-upload",{ref:"fileUpload",on:{success:e.insertUpload}}):e._e()],2)},Oy=[],py=di(fy,dy,Oy,!1,null,null,null,null);const gy=py.exports;window.panel.plugin("fabianmichael/markdown-field",{components:{"k-markdown-input":gy},blocks:{markdown:od},fields:{markdown:Bo},icons:{"horizontal-rule":'',"special-chars":'',pagelink:'',h1:'',h2:'',h3:'',h4:'',h5:'',h6:'',footnote:'',highlight:'',eraser:''}})})(); diff --git a/src/components/MarkdownInput.vue b/src/components/MarkdownInput.vue index 6bc66ce..f48ebc8 100644 --- a/src/components/MarkdownInput.vue +++ b/src/components/MarkdownInput.vue @@ -329,7 +329,7 @@ export default { uploadFile() { this.$refs.fileUpload.open({ - url: "/api/" + this.endpoints.field + "/upload", + url: this.$urls.api + '/' + this.endpoints.field + '/upload', multiple: false, }); },