Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added resolveFileUrl block ID field #1107

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const audioRender = (

const audio = document.createElement("audio");
audio.className = "bn-audio";
editor.resolveFileUrl(block.props.url).then((downloadUrl) => {
editor.resolveFileUrl(block.props.url, block.id).then((downloadUrl) => {
audio.src = downloadUrl;
});
audio.controls = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const imageRender = (

const image = document.createElement("img");
image.className = "bn-visual-media";
editor.resolveFileUrl(block.props.url).then((downloadUrl) => {
editor.resolveFileUrl(block.props.url, block.id).then((downloadUrl) => {
image.src = downloadUrl;
});
image.alt = block.props.name || block.props.caption || "BlockNote image";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ export const videoRender = (

const video = document.createElement("video");
video.className = "bn-visual-media";
video.src = block.props.url;
editor.resolveFileUrl(block.props.url, block.id).then((downloadUrl) => {
video.src = downloadUrl;
});
video.controls = true;
video.contentEditable = "false";
video.draggable = false;
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/editor/BlockNoteEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export type BlockNoteEditorOptions<
* implementing custom protocols / schemes
* @returns The URL that's
*/
resolveFileUrl: (url: string) => Promise<string>;
resolveFileUrl: (url: string, blockId?: string) => Promise<string>;

/**
* When enabled, allows for collaboration between multiple users.
Expand Down Expand Up @@ -282,7 +282,10 @@ export class BlockNoteEditor<
private onUploadStartCallbacks: ((blockId?: string) => void)[] = [];
private onUploadEndCallbacks: ((blockId?: string) => void)[] = [];

public readonly resolveFileUrl: (url: string) => Promise<string>;
public readonly resolveFileUrl: (
url: string,
blockId?: string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this parameter be required? Or are there cases when it's undefined? If so, I think we should document those

) => Promise<string>;

public get pmSchema() {
return this._pmSchema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const AudioPreview = (
"contentRef"
>
) => {
const resolved = useResolveUrl(props.block.props.url!);
const resolved = useResolveUrl(props.block.props.url!, props.block.id);

if (resolved.loadingState === "loading") {
return null;
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/blocks/FileBlockContent/useResolveUrl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEffect, useState } from "react";
import { useBlockNoteEditor } from "../../hooks/useBlockNoteEditor";
import { BlockSchema, InlineContentSchema, StyleSchema } from "@blocknote/core";

export function useResolveUrl(fetchUrl: string) {
export function useResolveUrl(fetchUrl: string, blockId?: string) {
const editor = useBlockNoteEditor<
BlockSchema,
InlineContentSchema,
Expand All @@ -21,7 +21,7 @@ export function useResolveUrl(fetchUrl: string) {
setLoadingState("loading");

try {
url = await editor.resolveFileUrl(fetchUrl);
url = await editor.resolveFileUrl(fetchUrl, blockId);
} catch (error) {
setLoadingState("error");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const ImagePreview = (
)
);

const resolved = useResolveUrl(props.block.props.url!);
const resolved = useResolveUrl(props.block.props.url!, props.block.id);

if (resolved.loadingState === "loading") {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const VideoPreview = (
)
);

const resolved = useResolveUrl(props.block.props.url!);
const resolved = useResolveUrl(props.block.props.url!, props.block.id);

if (resolved.loadingState === "loading") {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const UploadTab = <

if (editor.uploadFile !== undefined) {
try {
let updateData = await editor.uploadFile(file);
let updateData = await editor.uploadFile(file, block.id);
if (typeof updateData === "string") {
// received a url
updateData = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const FileDownloadButton = () => {
if (fileBlock && fileBlock.props.url) {
editor.focus();
editor
.resolveFileUrl(fileBlock.props.url)
.resolveFileUrl(fileBlock.props.url, fileBlock.id)
.then((downloadUrl) =>
window.open(sanitizeUrl(downloadUrl, window.location.href))
);
Expand Down
Loading