Skip to content

Commit

Permalink
fix: get root path util #16
Browse files Browse the repository at this point in the history
  • Loading branch information
gbicou committed Apr 24, 2023
1 parent b259ff2 commit 090daa1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/composables/image.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<string, any>) {
const assetUrl = 'http://0.0.0.0:8055/assets/' + image.id;
const assetUrl = getPublicURL() + "assets/" + image.id;

imageSelection.value = {
imageUrl: assetUrl,
Expand Down
26 changes: 26 additions & 0 deletions src/utils/get-root-path.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 090daa1

Please sign in to comment.