diff --git a/resources/js/components/fieldtypes/bard/BardFieldtype.vue b/resources/js/components/fieldtypes/bard/BardFieldtype.vue index 8fac6b5479f..6c470c009ed 100644 --- a/resources/js/components/fieldtypes/bard/BardFieldtype.vue +++ b/resources/js/components/fieldtypes/bard/BardFieldtype.vue @@ -492,12 +492,12 @@ export default { this.loadingSet = handle; this.fetchSet(handle) - .then(data => this._addSet(handle, index, data)) + .then(data => this._addSet(handle, data)) .catch(() => this.$toast.error(__('Something went wrong'))) .finally(() => this.loadingSet = null); }, - _addSet(handle, index, data) { + _addSet(handle, data) { const id = uniqid(); const deepCopy = JSON.parse(JSON.stringify(data.defaults)); const values = Object.assign({}, { type: handle }, deepCopy); @@ -551,7 +551,9 @@ export default { return this.fieldPathKeys .map((key, index) => { if (Number.isInteger(parseInt(key))) { - return data_get(this.publishContainer.values, this.fieldPathKeys.slice(0, index + 1).join('.'))?.attrs?.values.type; + let setValues = data_get(this.publishContainer.values, this.fieldPathKeys.slice(0, index + 1).join('.')); + + return setValues.attrs?.values.type || setValues.type; } return key; @@ -580,13 +582,18 @@ export default { }); }, - pasteSet(attrs) { + async pasteSet(attrs) { const old_id = attrs.id; const id = uniqid(); const enabled = attrs.enabled; const values = Object.assign({}, attrs.values); - this.updateSetMeta(id, this.meta.existing[old_id] || this.meta.defaults[values.type] || {}); + if (this.meta.existing[old_id]) { + this.updateSetMeta(id, this.meta.existing[old_id]); + } else { + const data = await this.fetchSet(values.type); + this.updateSetMeta(id, data.new); + } return { id, enabled, values }; }, diff --git a/resources/js/components/fieldtypes/bard/Set.js b/resources/js/components/fieldtypes/bard/Set.js index a9f43523411..149f2bdd46f 100644 --- a/resources/js/components/fieldtypes/bard/Set.js +++ b/resources/js/components/fieldtypes/bard/Set.js @@ -106,20 +106,36 @@ export const Set = Node.create({ new Plugin({ key: new PluginKey('setPastedTransformer'), props: { - transformPasted: (slice) => { - const { content } = slice.content; - return new Slice( - Fragment.fromArray( - content.map((node) => { - if (node.type === type) { - return node.type.create(bard.pasteSet(node.attrs)); - } - return node.copy(node.content); - }), - ), - slice.openStart, - slice.openEnd, - ); + handlePaste: (view, event, slice) => { + let hasSetNodes = false; + slice.content.forEach((node) => { + if (node.type === type) hasSetNodes = true; + }); + + if (!hasSetNodes) return false; + + (async () => { + const content = []; + for (const node of slice.content.content) { + if (node.type === type) { + const newAttrs = await bard.pasteSet(node.attrs); + content.push(node.type.create(newAttrs)); + } else { + content.push(node); + } + } + + const newSlice = new Slice( + Fragment.fromArray(content), + slice.openStart, + slice.openEnd, + ); + + const tr = view.state.tr.replaceSelection(newSlice); + view.dispatch(tr); + })(); + + return true; }, }, }), diff --git a/resources/js/components/fieldtypes/replicator/Replicator.vue b/resources/js/components/fieldtypes/replicator/Replicator.vue index 49b6f1af3a4..39fdd154516 100644 --- a/resources/js/components/fieldtypes/replicator/Replicator.vue +++ b/resources/js/components/fieldtypes/replicator/Replicator.vue @@ -266,7 +266,9 @@ export default { return this.fieldPathKeys .map((key, index) => { if (Number.isInteger(parseInt(key))) { - return data_get(this.publishContainer.values, this.fieldPathKeys.slice(0, index + 1).join('.'))?.type; + let setValues = data_get(this.publishContainer.values, this.fieldPathKeys.slice(0, index + 1).join('.')); + + return setValues.attrs?.values.type || setValues.type; } return key;