Skip to content

Commit b011100

Browse files
fix: refactor uploader
1 parent cea6f75 commit b011100

File tree

3 files changed

+45
-45
lines changed

3 files changed

+45
-45
lines changed

packages/editor/src/core/extensions/callout/logo-selector.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,27 +53,29 @@ export function CalloutBlockLogoSelector(props: Props) {
5353
};
5454
if (val.type === "emoji") {
5555
// val.value is now a string in decimal format (e.g. "128512")
56+
const emojiValue = val.value as string;
5657
newLogoValue = {
57-
"data-emoji-unicode": val.value,
58+
"data-emoji-unicode": emojiValue,
5859
"data-emoji-url": undefined,
5960
};
6061
newLogoValueToStoreInLocalStorage = {
6162
in_use: "emoji",
6263
emoji: {
63-
value: val.value,
64+
value: emojiValue,
6465
url: undefined,
6566
},
6667
};
6768
} else if (val.type === "icon") {
69+
const iconValue = val.value as { name: string; color: string };
6870
newLogoValue = {
69-
"data-icon-name": val.value.name,
70-
"data-icon-color": val.value.color,
71+
"data-icon-name": iconValue.name,
72+
"data-icon-color": iconValue.color,
7173
};
7274
newLogoValueToStoreInLocalStorage = {
7375
in_use: "icon",
7476
icon: {
75-
name: val.value.name,
76-
color: val.value.color,
77+
name: iconValue.name,
78+
color: iconValue.color,
7779
},
7880
};
7981
}

packages/editor/src/core/extensions/custom-image/components/uploader.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export function CustomImageUploader(props: CustomImageUploaderProps) {
4040
// refs
4141
const fileInputRef = useRef<HTMLInputElement>(null);
4242
const hasTriggeredFilePickerRef = useRef(false);
43+
const hasTriedUploadingOnMountRef = useRef(false);
4344
const { id: imageEntityId } = node.attrs;
4445
// derived values
4546
const imageComponentImageFileMap = useMemo(() => getImageComponentImageFileMap(editor), [editor]);
@@ -124,17 +125,16 @@ export function CustomImageUploader(props: CustomImageUploaderProps) {
124125
uploader: uploadFile,
125126
});
126127

127-
// the meta data of the image component
128-
const meta = useMemo(
129-
() => imageComponentImageFileMap?.get(imageEntityId ?? ""),
130-
[imageComponentImageFileMap, imageEntityId]
131-
);
132-
133128
// after the image component is mounted we start the upload process based on
134129
// it's uploaded
135130
useEffect(() => {
131+
if (hasTriedUploadingOnMountRef.current) return;
132+
133+
// the meta data of the image component
134+
const meta = imageComponentImageFileMap?.get(imageEntityId ?? "");
136135
if (meta) {
137136
if (meta.event === "drop" && "file" in meta) {
137+
hasTriedUploadingOnMountRef.current = true;
138138
uploadFile(meta.file);
139139
} else if (meta.event === "insert" && fileInputRef.current && !hasTriggeredFilePickerRef.current) {
140140
if (meta.hasOpenedFileInputOnce) return;
@@ -144,8 +144,10 @@ export function CustomImageUploader(props: CustomImageUploaderProps) {
144144
hasTriggeredFilePickerRef.current = true;
145145
imageComponentImageFileMap?.set(imageEntityId ?? "", { ...meta, hasOpenedFileInputOnce: true });
146146
}
147+
} else {
148+
hasTriedUploadingOnMountRef.current = true;
147149
}
148-
}, [meta, uploadFile, imageComponentImageFileMap, imageEntityId, isTouchDevice]);
150+
}, [imageEntityId, isTouchDevice, uploadFile, imageComponentImageFileMap]);
149151

150152
const onFileChange = useCallback(
151153
async (e: ChangeEvent<HTMLInputElement>) => {

packages/editor/src/core/plugins/file/delete.ts

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const TrackFileDeletionPlugin = (editor: Editor, deleteHandler: TFileHand
2121
[nodeType: string]: Set<string> | undefined;
2222
} = {};
2323
if (!transactions.some((tr) => tr.docChanged)) return null;
24+
if (transactions.some((tr) => tr.getMeta(CORE_EDITOR_META.SKIP_FILE_DELETION))) return null;
2425

2526
newState.doc.descendants((node) => {
2627
const nodeType = node.type.name as keyof NodeFileMapType;
@@ -34,40 +35,35 @@ export const TrackFileDeletionPlugin = (editor: Editor, deleteHandler: TFileHand
3435
}
3536
});
3637

37-
transactions.forEach((transaction) => {
38-
// if the transaction has meta of skipFileDeletion set to true, then return (like while clearing the editor content programmatically)
39-
if (transaction.getMeta(CORE_EDITOR_META.SKIP_FILE_DELETION)) return;
38+
const removedFiles: TFileNode[] = [];
4039

41-
const removedFiles: TFileNode[] = [];
42-
43-
// iterate through all the nodes in the old state
44-
oldState.doc.descendants((node) => {
45-
const nodeType = node.type.name as keyof NodeFileMapType;
46-
const isAValidNode = NODE_FILE_MAP[nodeType];
47-
// if the node doesn't match, then return as no point in checking
48-
if (!isAValidNode) return;
49-
// Check if the node has been deleted or replaced
50-
if (!newFileSources[nodeType]?.has(node.attrs.src)) {
51-
removedFiles.push(node as TFileNode);
52-
}
53-
});
40+
// iterate through all the nodes in the old state
41+
oldState.doc.descendants((node) => {
42+
const nodeType = node.type.name as keyof NodeFileMapType;
43+
const isAValidNode = NODE_FILE_MAP[nodeType];
44+
// if the node doesn't match, then return as no point in checking
45+
if (!isAValidNode) return;
46+
// Check if the node has been deleted or replaced
47+
if (!newFileSources[nodeType]?.has(node.attrs.src)) {
48+
removedFiles.push(node as TFileNode);
49+
}
50+
});
5451

55-
removedFiles.forEach(async (node) => {
56-
const nodeType = node.type.name as keyof NodeFileMapType;
57-
const src = node.attrs.src;
58-
const nodeFileSetDetails = NODE_FILE_MAP[nodeType];
59-
if (!nodeFileSetDetails || !src) return;
60-
try {
61-
editor.storage[nodeType]?.[nodeFileSetDetails.fileSetName]?.set(src, true);
62-
// update assets list storage value
63-
editor.commands.updateAssetsList?.({
64-
idToRemove: node.attrs.id,
65-
});
66-
await deleteHandler(src);
67-
} catch (error) {
68-
console.error("Error deleting file via delete utility plugin:", error);
69-
}
70-
});
52+
removedFiles.forEach(async (node) => {
53+
const nodeType = node.type.name as keyof NodeFileMapType;
54+
const src = node.attrs.src;
55+
const nodeFileSetDetails = NODE_FILE_MAP[nodeType];
56+
if (!nodeFileSetDetails || !src) return;
57+
try {
58+
editor.storage[nodeType]?.[nodeFileSetDetails.fileSetName]?.set(src, true);
59+
// update assets list storage value
60+
editor.commands.updateAssetsList?.({
61+
idToRemove: node.attrs.id,
62+
});
63+
await deleteHandler(src);
64+
} catch (error) {
65+
console.error("Error deleting file via delete utility plugin:", error);
66+
}
7167
});
7268

7369
return null;

0 commit comments

Comments
 (0)