import { reactive } from 'vue';
/**
* Bildauswahl als Promise – für Editoren (Pagebuilder, Neuigkeiten …):
*
* const picker = useMediaPicker({ upload: (file, onProgress) => …, contextPaths: () => [...] });
* const items = await picker.pick({ multiple: true }); // null bei Abbruch
* // items: [{ id, path, url, alt, caption, width, height }]
*
* Im Template einmal einbinden.
*
* @param {{ upload?: Function, contextPaths?: () => string[], contextLabel?: string }} defaults
*/
export function useMediaPicker(defaults = {}) {
const state = reactive({
open: false,
multiple: false,
title: '',
contextPaths: [],
contextLabel: defaults.contextLabel ?? 'Hier verwendet',
upload: defaults.upload ?? null,
resolve: null,
});
/**
* @param {{ multiple?: boolean, title?: string, contextPaths?: string[] }} options
* @returns {Promise | null>}
*/
function pick(options = {}) {
state.resolve?.(null);
return new Promise((resolve) => {
Object.assign(state, {
multiple: !!options.multiple,
title: options.title ?? '',
contextPaths: options.contextPaths ?? defaults.contextPaths?.() ?? [],
resolve,
open: true,
});
});
}
function finish(items) {
const resolve = state.resolve;
state.resolve = null;
state.open = false;
resolve?.(items);
}
return { state, pick, finish };
}