Skip to content

Commit

Permalink
Merge pull request #5678 from getkirby/v4/fix/5671-blocks-paste
Browse files Browse the repository at this point in the history
Fix pasting blocks
  • Loading branch information
distantnative authored Sep 24, 2023
2 parents 2a574d8 + 0e5e7c9 commit ad7de06
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
10 changes: 8 additions & 2 deletions panel/src/components/Forms/Blocks/BlockSelector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,16 @@ export default {
}
},
created() {
this.$events.on("paste", this.close);
this.$events.on("paste", this.paste);
},
destroyed() {
this.$events.off("paste", this.close);
this.$events.off("paste", this.paste);
},
methods: {
paste(e) {
this.$emit("paste", e);
this.close();
}
}
};
</script>
Expand Down
39 changes: 14 additions & 25 deletions panel/src/components/Forms/Blocks/Blocks.vue
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,13 @@ export default {
fieldsets: this.fieldsets
},
on: {
open: () => {
this.isPasteable = true;
},
close: () => {
this.isPasteable = false;
},
submit: (type) => {
this.add(type, index);
this.$panel.dialog.close();
},
paste: this.paste
paste: (e) => {
this.paste(e, index);
}
}
});
},
Expand All @@ -211,12 +207,6 @@ export default {
headline: this.$t("field.blocks.changeType")
},
on: {
open: () => {
this.isPasteable = true;
},
close: () => {
this.isPasteable = false;
},
submit: (type) => {
this.convert(type, block);
this.$panel.dialog.close();
Expand Down Expand Up @@ -513,17 +503,12 @@ export default {
}
},
onPaste(e) {
// enable pasting when the block selector is open
if (this.isPasteable === true) {
return this.paste(e);
}
// never paste blocks when the focus is in an input element
if (this.isInputEvent(e) === true) {
return false;
}
// not when any other dialogs or drawers are open
// not when any dialogs or drawers are open
if (this.isEditing === true || this.$panel.dialog.isOpen === true) {
return false;
}
Expand All @@ -539,7 +524,7 @@ export default {
open(block) {
this.$refs["block-" + block.id]?.[0].open();
},
async paste(e) {
async paste(e, index) {
const html = this.$helper.clipboard.read(e);
// pass html or plain text to the paste endpoint to convert it to blocks
Expand All @@ -548,11 +533,15 @@ export default {
});
// get the index
let lastItem = this.selected[this.selected.length - 1];
let lastIndex = this.findIndex(lastItem);
if (index === undefined) {
let item = this.selected[this.selected.length - 1];
index = this.findIndex(item);
if (index === -1) {
index = this.blocks.length;
}
if (lastIndex === -1) {
lastIndex = this.blocks.length;
index++;
}
// don't add blocks that exceed the maximum limit
Expand All @@ -561,7 +550,7 @@ export default {
blocks = blocks.slice(0, max);
}
this.blocks.splice(lastIndex + 1, 0, ...blocks);
this.blocks.splice(index, 0, ...blocks);
this.save();
// a sign that it has been pasted
Expand Down

0 comments on commit ad7de06

Please sign in to comment.