Skip to content

Commit 36d9b9e

Browse files
committed
fix embed upload endpoint
1 parent 08d0db9 commit 36d9b9e

9 files changed

Lines changed: 56 additions & 17 deletions

File tree

resources/js/form/components/Field.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import Editor from "./fields/editor/Editor.vue";
99
import Geolocation from "./fields/geolocation/Geolocation.vue";
1010
import Html from "./fields/Html.vue";
11-
import List from "./fields/List.vue";
11+
import List from "./fields/list/List.vue";
1212
import Number from "./fields/Number.vue";
1313
import Select from "./fields/select/Select.vue";
1414
import Tags from "./fields/Tags.vue";

resources/js/form/components/fields/Autocomplete.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import { useFullTextSearch } from "@/composables/useFullTextSearch";
2727
import { useRemoteAutocomplete } from "@/composables/useRemoteAutocomplete";
2828
import { useFieldContainerData } from "@/form/useFieldContainerData";
29+
import { useParentListField } from "@/form/components/fields/list/useParentListField";
2930
3031
const props = defineProps<FormFieldProps<FormAutocompleteLocalFieldData | FormAutocompleteRemoteFieldData>>();
3132
const emit = defineEmits<FormFieldEmits<FormAutocompleteLocalFieldData | FormAutocompleteRemoteFieldData>>();
@@ -43,13 +44,16 @@
4344
searchKeys: props.field.mode === 'local' ? props.field.searchKeys : [],
4445
}
4546
);
47+
const parentListField = useParentListField();
4648
const fieldContainerData = useFieldContainerData(form);
4749
const { loading, search: remoteSearch } = useRemoteAutocomplete(({ query, signal, onSuccess, onError }) => {
4850
const field = props.field as FormAutocompleteRemoteFieldData;
4951
return api.post(
5052
route('code16.sharp.api.form.autocomplete.index', {
5153
entityKey: form.entityKey,
52-
autocompleteFieldKey: props.parentListField ? `${props.parentListField.key}.${field.key}` : field.key,
54+
autocompleteFieldKey: parentListField && parentListField.form === form
55+
? `${parentListField.props.field.key}.${field.key}`
56+
: field.key,
5357
endpoint: field.remoteEndpoint,
5458
search: query,
5559
...fieldContainerData,

resources/js/form/components/fields/editor/Editor.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@
8686
uploadModal,
8787
embedManager,
8888
embedModal,
89-
} satisfies ParentEditor);
89+
form,
90+
});
9091
9192
watch(() => [embedManager.contentEmbeds, uploadManager.contentUploads], () => {
9293
emit('input', {

resources/js/form/components/fields/editor/extensions/upload/UploadNode.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
readOnly: parentEditor.props.field.readOnly,
7777
}"
7878
:field-error-key="`${parentEditor.props.fieldErrorKey}-upload-${props.node.attrs['data-key']}`"
79-
:parent-list-field="parentEditor.props.parentListField"
8079
:editor-field="parentEditor.props.field"
8180
:value="upload?.file"
8281
as-editor-embed

resources/js/form/components/fields/editor/useParentEditor.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ import EditorUploadModal from "@/form/components/fields/editor/extensions/upload
88
import EditorEmbedModal from "@/form/components/fields/editor/extensions/embed/EditorEmbedModal.vue";
99

1010
/**
11-
* @see import('./Editor.vue') -> provide('editor')
11+
* @see import('./Editor.vue')
1212
*/
1313
export type ParentEditor = {
1414
props: FormFieldProps<FormEditorFieldData>,
1515
embedManager: ContentEmbedManager<Form>,
1616
embedModal: Ref<InstanceType<typeof EditorEmbedModal>>
1717
uploadManager: ContentUploadManager<Form>,
1818
uploadModal: Ref<InstanceType<typeof EditorUploadModal>>,
19+
form: Form,
1920
};
2021

2122
export function useParentEditor() {
22-
return inject<ParentEditor>('editor');
23+
return inject<ParentEditor>('editor', null);
2324
}

resources/js/form/components/fields/List.vue renamed to resources/js/form/components/fields/list/List.vue

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { useParentForm } from "@/form/useParentForm";
44
import { FormFieldData, FormListFieldData, FormUploadFieldData, FormUploadFieldValueData } from "@/types";
55
import { getDependantFieldsResetData } from "@/form/util";
6-
import { computed, nextTick, ref, watch, watchEffect } from "vue";
6+
import { computed, nextTick, provide, ref, watch, watchEffect } from "vue";
77
import { Button, buttonVariants } from '@/components/ui/button';
88
import { showAlert } from "@/utils/dialogs";
99
import { FieldMeta, FieldsMeta, FormFieldEmitInputOptions, FormFieldEmits, FormFieldProps } from "@/form/types";
@@ -22,11 +22,18 @@
2222
import { useSortable } from "@vueuse/integrations/useSortable";
2323
import { useEventListener, watchArray } from "@vueuse/core";
2424
import { FormEvents } from "@/form/Form";
25+
import { ParentListField } from "@/form/components/fields/list/useParentListField";
2526
2627
const props = defineProps<FormFieldProps<FormListFieldData>>();
2728
const emit = defineEmits<FormFieldEmits<FormListFieldData>>();
2829
2930
const form = useParentForm();
31+
32+
provide<ParentListField>('listField', {
33+
props,
34+
form,
35+
});
36+
3037
const canAddItem = computed(() => {
3138
const { field, value } = props;
3239
return field.addable &&
@@ -226,7 +233,6 @@
226233
:field="form.getField(itemFieldLayout.key, field.itemFields, item, props.field.readOnly)"
227234
:field-layout="itemFieldLayout"
228235
:field-error-key="`${field.key}.${item[errorIndex] ?? item[itemKey]}.${itemFieldLayout.key}`"
229-
:parent-list-field="field"
230236
:value="item[itemFieldLayout.key]"
231237
:locale="(form.getMeta(`${field.key}.${item[itemKey]}.${itemFieldLayout.key}`) as FieldMeta)?.locale ?? form.defaultLocale"
232238
:parent-data="item"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { FormFieldProps } from "@/form/types";
2+
import { FormListFieldData } from "@/types";
3+
import { inject } from "vue";
4+
import { Form } from "@/form/Form";
5+
6+
export type ParentListField = {
7+
props: FormFieldProps<FormListFieldData>,
8+
form: Form,
9+
}
10+
11+
/**
12+
* @see import('./List.vue')
13+
*/
14+
export function useParentListField() {
15+
return inject<ParentListField>('listField', null);
16+
}

resources/js/form/components/fields/upload/Upload.vue

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@
4242
import { rotate, rotateTo } from "@/form/components/fields/upload/util/rotate";
4343
import { createThumbnail } from "@/form/components/fields/upload/util/thumbnail";
4444
import { useFieldContainerData } from "@/form/useFieldContainerData";
45+
import { useParentEditor } from "@/form/components/fields/editor/useParentEditor";
46+
import { useParentListField } from "@/form/components/fields/list/useParentListField";
4547
4648
const props = defineProps<FormFieldProps<FormUploadFieldData> & {
47-
editorField?: FormEditorFieldData,
4849
asEditorEmbed?: boolean,
4950
legend?: string,
5051
dropdownEditLabel?: string,
@@ -68,6 +69,8 @@
6869
(e: 'edit', event: CustomEvent): void
6970
}>();
7071
const form = useParentForm();
72+
const parentEditor = useParentEditor();
73+
const parentListField = useParentListField();
7174
const transformedImg = ref<string>();
7275
const persistedEditableImg = ref<string>();
7376
const playablePreviewUrl = ref<string>();
@@ -76,6 +79,22 @@
7679
return props.value && canTransform(props.value.name, props.value.mime_type) && !props.hasError
7780
|| !!props.dropdownEditLabel;
7881
});
82+
const fieldContainerData = useFieldContainerData(form);
83+
const uploadEndpoint = computed(() => {
84+
// we check form equality because the field may be in an editor embed
85+
// so parentEditor can be the embed parent editor and not an embed form field
86+
let uploadFieldKey = parentEditor && parentEditor.form === form
87+
? parentEditor.props.field.key
88+
: props.field.key;
89+
if(parentListField && parentListField.form === form) {
90+
uploadFieldKey = `${parentListField.props.field.key}.${uploadFieldKey}`;
91+
}
92+
return route('code16.sharp.api.form.upload', {
93+
entityKey: form.entityKey,
94+
uploadFieldKey,
95+
...fieldContainerData,
96+
});
97+
});
7998
const uppy = new Uppy({
8099
id: props.fieldErrorKey,
81100
restrictions: {
@@ -95,13 +114,7 @@
95114
autoProceed: true,
96115
})
97116
.use(XHRUpload, {
98-
endpoint: route('code16.sharp.api.form.upload', {
99-
entityKey: form.entityKey,
100-
uploadFieldKey: props.parentListField
101-
? `${props.parentListField.key}.${props.editorField?.key ?? props.field.key}`
102-
: props.editorField?.key ?? props.field.key,
103-
...useFieldContainerData(form),
104-
}),
117+
endpoint: uploadEndpoint.value,
105118
fieldName: 'file',
106119
headers: {
107120
'Accept': 'application/json',

resources/js/form/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export type WithDynamicAttributesApplied<Data extends FormFieldData> =
2323

2424
export type FormFieldProps<Data extends FormFieldData = FormFieldData, Value = Data['value']> = {
2525
field: WithDynamicAttributesApplied<Data>,
26-
parentListField?: FormListFieldData,
2726
fieldLayout?: LayoutFieldData,
2827
fieldErrorKey?: string,
2928
parentData?: FormFieldData | FormListFieldData['value'][number],

0 commit comments

Comments
 (0)