Skip to content
This repository has been archived by the owner on Jun 4, 2022. It is now read-only.

Commit

Permalink
add unique event names
Browse files Browse the repository at this point in the history
add unique event names to support repeating fields.
  • Loading branch information
bayareawebpro committed Aug 18, 2021
1 parent c515a90 commit 2264022
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 30 deletions.
2 changes: 1 addition & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/js/field.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion resources/js/ckeditor/config/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export default {
'imageTextAlternative',
],
styles: [
'full',
'alignLeft',
'alignCenter',
'alignRight',
'full',
]
}
}
6 changes: 4 additions & 2 deletions resources/js/ckeditor/plugins/MediaBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default class MediaBrowser{
*/
openModal(){
this.saveSelection()

Nova.$emit(`ckeditor:media:${this.attribute}`)
}

Expand Down Expand Up @@ -105,7 +106,7 @@ export default class MediaBrowser{
* @return {Boolean}
*/
get hasSelectedWidget(){
return (this.selected && this.selected.name === 'image')
return (this.selected && this.selected.name === 'imageBlock')
}

/**
Expand All @@ -117,14 +118,15 @@ export default class MediaBrowser{
this.model.change((writer) => {
items.forEach(({file, url}, index) => {
if(index === 0 && this.hasSelectedWidget) {
return writer.setAttributes({src: url, alt: file, imageCaption: file}, selected)
return writer.setAttributes({src: url, alt: file, imageCaption: file}, this.selected)
}
return this.model.insertContent(
writer.createElement('imageBlock', {src: url, alt: file, imageCaption: file}),
this.model.document.selection.getLastPosition()
)
})
})
this.clearSelection()
}

/**
Expand Down
18 changes: 12 additions & 6 deletions resources/js/components/editor-field.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import SnippetBrowser from "./snippet-browser"
import MediaBrowser from "./media-browser"
import LinkBrowser from "./link-browser"
import HasUUID from "./mixins/HasUUID"
import {FormField, HandlesValidationErrors} from 'laravel-nova'
export default {
components: {SnippetBrowser, LinkBrowser,MediaBrowser},
mixins: [FormField, HandlesValidationErrors],
mixins: [FormField, HandlesValidationErrors,HasUUID],
props: ['resourceName', 'resourceId', 'field','toolbar'],
methods: {
setInitialValue() {
Expand All @@ -25,17 +26,21 @@
},
handleEditorSync(){
this.handleChange(this.$options.editor.getData())
}
},
},
created() {
this.$options.uuid = this.uuid()
},
mounted() {
CkEditor.create(this.$refs.editor,{
attribute: this.field.attribute,
attribute: this.$options.uuid,
linkBrowser: this.field.linkBrowser,
mediaBrowser: this.field.mediaBrowser,
snippetBrowser: this.field.snippetBrowser,
toolbar:{items: this.field.toolbar}
}).then((editor) => {
const {editing, model} = this.$options.editor = editor
//Prevent QuestionMark & Slash from triggering Nova Search.
editing.view.document.on('keydown', this.handleEditorEvents, {
priority: 'highest'
Expand Down Expand Up @@ -72,14 +77,15 @@
:value="value"
/>
<link-browser
:attribute="field.attribute"
:field-key="$options.uuid"
/>
<media-browser
:attribute="field.attribute"
@select="$options.editor.execute('mediaBrowser', $event)"
:field-key="$options.uuid"
:multiple="true"
/>
<snippet-browser
:attribute="field.attribute"
:field-key="$options.uuid"
:snippets="field.snippetBrowser"
/>
</template>
Expand Down
9 changes: 4 additions & 5 deletions resources/js/components/link-browser.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
name: "link-browser",
mixins:[interactsWithResources],
components: {loading, modal},
props: {attribute: {default: ()=>'content'}},
props: {fieldKey: {default: ()=>'content'}},
data() {
return {
searchTerm: '',
Expand All @@ -18,7 +18,7 @@
},
computed:{
event(){
return `ckeditor:link:${this.attribute}`
return `ckeditor:link:${this.fieldKey}`
},
},
methods: {
Expand Down Expand Up @@ -66,16 +66,15 @@
* Close the Modal
* If the user focuses another instance of the editor, close the modal.
*/
close(field) {
if(field !== this.attribute){
close(fieldKey) {
if(fieldKey !== this.fieldKey){
this.isVisible = false
}
},
},
created() {
Nova.$on(this.event, this.open)
Nova.$on(`ckeditor:focused`, this.close)
//this.toggle()
},
beforeDestroy() {
Nova.$off(this.event, this.open)
Expand Down
8 changes: 4 additions & 4 deletions resources/js/components/media-browser.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
components: {loading, modal},
mixins: [interactsWithResources],
props: {
attribute: {default: () => 'content'},
fieldKey: {default: () => 'content'},
multiple: {default: () => true},
},
data: () => ({
Expand All @@ -27,7 +27,7 @@
}),
computed: {
event() {
return `ckeditor:media:${this.attribute}`
return `ckeditor:media:${this.fieldKey}`
}
},
methods: {
Expand Down Expand Up @@ -141,8 +141,8 @@
* Close the Modal
* If the user focuses another instance of the editor, close the modal.
*/
close(field) {
if(field !== this.attribute){
close(fieldKey) {
if(fieldKey !== this.fieldKey){
this.isVisible = false
}
},
Expand Down
12 changes: 6 additions & 6 deletions resources/js/components/media-field.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<script>
import spinner from './../assets/spinner'
import MediaBrowser from "./media-browser"
import HasUUID from "./mixins/HasUUID"
import interactsWithResources from "./mixins/interactsWithResources"
import { FormField, HandlesValidationErrors } from 'laravel-nova'
export default {
props: ['resourceName', 'resourceId', 'field'],
mixins: [FormField, HandlesValidationErrors,interactsWithResources],
mixins: [FormField, HandlesValidationErrors,interactsWithResources, HasUUID],
components: {MediaBrowser},
data: ()=>({preview: null}),
computed: {
event(){
return `ckeditor:media:${this.field.attribute}`
return `ckeditor:media:${this.$options.uuid}`
},
},
methods: {
Expand All @@ -37,10 +38,9 @@ export default {
Nova.$emit(this.event)
},
},
beforeCreate() {
this.$options.spinner = spinner
},
created(){
this.$options.spinner = spinner
this.$options.uuid = this.uuid()
Nova.$on(`${this.event}:write`, this.handleChange)
},
beforeDestroy(){
Expand Down Expand Up @@ -81,7 +81,7 @@ export default {
<p v-if="hasError" class="my-2 text-danger">
{{ firstError }}
</p>
<media-browser :attribute="field.attribute" :multiple="false"/>
<media-browser :field-key="$options.uuid" :multiple="false"/>
</template>
</default-field>
</template>
7 changes: 7 additions & 0 deletions resources/js/components/mixins/hasUUID.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
methods:{
uuid() {
return crypto.getRandomValues(new Uint32Array(4)).join('-')
},
},
}
8 changes: 4 additions & 4 deletions resources/js/components/snippet-browser.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
name: "snippet-browser",
components: {modal},
props: {
attribute: {default: () => 'content'},
fieldKey: {default: () => 'content'},
snippets: {default: () => ([])},
},
data: () => ({
Expand All @@ -14,7 +14,7 @@ export default {
}),
computed: {
event() {
return `ckeditor:snippets:${this.attribute}`
return `ckeditor:snippets:${this.fieldKey}`
}
},
methods: {
Expand All @@ -37,8 +37,8 @@ export default {
* Close the Modal
* If the user focuses another instance of the editor, close the modal.
*/
close(field) {
if (field !== this.attribute) {
close(fieldKey) {
if (fieldKey !== this.fieldKey) {
this.isVisible = false
}
},
Expand Down

0 comments on commit 2264022

Please sign in to comment.