From 090daa18a61596f728c5e7237f53a554bc6f35ce Mon Sep 17 00:00:00 2001 From: Benjamin VIELLARD Date: Mon, 24 Apr 2023 10:54:41 +0200 Subject: [PATCH] fix: get root path util #16 --- src/composables/image.ts | 9 +++------ src/utils/get-root-path.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 src/utils/get-root-path.ts diff --git a/src/composables/image.ts b/src/composables/image.ts index 011ee9d..13e0e61 100644 --- a/src/composables/image.ts +++ b/src/composables/image.ts @@ -1,5 +1,6 @@ import type { Editor } from "@tiptap/vue-3"; import { ref } from "vue"; +import { getPublicURL } from "../utils/get-root-path"; type ImageSelection = { imageUrl: string; @@ -26,16 +27,12 @@ export function useImage(editor: Editor) { } function imageSave() { - editor - .chain() - .focus() - .setImage({ src: imageSelection.value.imageUrl }) - .run(); + editor.chain().focus().setImage({ src: imageSelection.value.imageUrl }).run(); imageClose(); } function imageSelect(image: Record) { - const assetUrl = 'http://0.0.0.0:8055/assets/' + image.id; + const assetUrl = getPublicURL() + "assets/" + image.id; imageSelection.value = { imageUrl: assetUrl, diff --git a/src/utils/get-root-path.ts b/src/utils/get-root-path.ts new file mode 100644 index 0000000..ce78586 --- /dev/null +++ b/src/utils/get-root-path.ts @@ -0,0 +1,26 @@ +/** + * Get the API root location from the current window path + */ +export function getRootPath(): string { + return extract(window.location.pathname); +} + +/** + * Get the full API root URL from the current page href + */ +export function getPublicURL(): string { + return extract(window.location.href); +} + +/** + * Extract the root path of the admin app from a given input path/url + * + * @param path - Path or URL string of the current page + * @returns - Root URL of the Directus instance + */ +export function extract(path: string) { + const parts = path.split("/"); + const adminIndex = parts.indexOf("admin"); + const rootPath = parts.slice(0, adminIndex).join("/") + "/"; + return rootPath; +}