Compare commits

...

2 Commits

Author SHA1 Message Date
zsviczian
f5475bfde6 imagepath hook 2025-01-20 22:40:27 +01:00
zsviczian
5171978c37 Merge pull request #2217 from zsviczian/PDF-fix
Some checks failed
CodeQL / Analyze (javascript) (push) Has been cancelled
Pdf crop fix
2025-01-17 06:46:59 +01:00
5 changed files with 92 additions and 11 deletions

View File

@@ -105,6 +105,41 @@
*/ */
//ea.onFileCreateHook = (data) => {}; //ea.onFileCreateHook = (data) => {};
/**
* If set, this callback is triggered when a image is being saved in Excalidraw.
* You can use this callback to customize the naming and path of pasted images to avoid
* default names like "Pasted image 123147170.png" being saved in the attachments folder,
* and instead use more meaningful names based on the Excalidraw file or other criteria,
* plus save the image in a different folder.
*
* If the function returns null or undefined, the normal Excalidraw operation will continue
* with the excalidraw generated name and default path.
* If a filepath is returned, that will be used. Include the full Vault filepath and filename
* with the file extension.
* The currentImageName is the name of the image generated by excalidraw or provided during paste.
*
* @param data - An object containing the following properties:
* @property {string} [currentImageName] - Default name for the image.
* @property {string} drawingFilePath - The file path of the Excalidraw file where the image is being used.
*
* @returns {string} - The new filepath for the image including full vault path and extension.
*
* Example usage:
* ```
* onImageFilePathHook: (data) => {
* const { currentImageName, drawingFilePath } = data;
* const ext = currentImageName.split('.').pop();
* // Generate a new filepath based on the drawing file name and other criteria
* return `${drawingFileName} - ${currentImageName || 'image'}.${ext}`;
* }
* ```
* onImageFilePathHook: (data: {
* currentImageName: string; // Excalidraw generated name of the image, or the name received from the file system.
* drawingFilePath: string; // The full filepath of the Excalidraw file where the image is being used.
* }) => string = null;
*/
//ea.onImageFileNameHook = (data) => {};
/** /**
* If set, this callback is triggered whenever the active canvas color changes * If set, this callback is triggered whenever the active canvas color changes
* onCanvasColorChangeHook: ( * onCanvasColorChangeHook: (

File diff suppressed because one or more lines are too long

View File

@@ -2670,6 +2670,40 @@ export class ExcalidrawAutomate {
pointerPosition: { x: number; y: number }; //the pointer position on canvas pointerPosition: { x: number; y: number }; //the pointer position on canvas
}) => boolean = null; }) => boolean = null;
/**
* If set, this callback is triggered when a image is being saved in Excalidraw.
* You can use this callback to customize the naming and path of pasted images to avoid
* default names like "Pasted image 123147170.png" being saved in the attachments folder,
* and instead use more meaningful names based on the Excalidraw file or other criteria,
* plus save the image in a different folder.
*
* If the function returns null or undefined, the normal Excalidraw operation will continue
* with the excalidraw generated name and default path.
* If a filepath is returned, that will be used. Include the full Vault filepath and filename
* with the file extension.
* The currentImageName is the name of the image generated by excalidraw or provided during paste.
*
* @param data - An object containing the following properties:
* @property {string} [currentImageName] - Default name for the image.
* @property {string} drawingFilePath - The file path of the Excalidraw file where the image is being used.
*
* @returns {string} - The new filepath for the image including full vault path and extension.
*
* Example usage:
* ```
* onImageFilePathHook: (data) => {
* const { currentImageName, drawingFilePath } = data;
* // Generate a new filepath based on the drawing file name and other criteria
* const ext = currentImageName.split('.').pop();
* return `${drawingFileName} - ${currentImageName || 'image'}.${ext}`;
* }
* ```
*/
onImageFilePathHook: (data: {
currentImageName: string; // Excalidraw generated name of the image, or the name received from the file system.
drawingFilePath: string; // The full filepath of the Excalidraw file where the image is being used.
}) => string = null;
/** /**
* if set, this callback is triggered, when an Excalidraw file is opened * if set, this callback is triggered, when an Excalidraw file is opened
* You can use this callback in case you want to do something additional when the file is opened. * You can use this callback in case you want to do something additional when the file is opened.

View File

@@ -20,7 +20,7 @@ import {
loadSceneFonts, loadSceneFonts,
} from "../constants/constants"; } from "../constants/constants";
import ExcalidrawPlugin from "../core/main"; import ExcalidrawPlugin from "../core/main";
import { TextMode } from "../view/ExcalidrawView"; import ExcalidrawView, { TextMode } from "../view/ExcalidrawView";
import { import {
addAppendUpdateCustomData, addAppendUpdateCustomData,
compress, compress,
@@ -52,7 +52,7 @@ import { getMermaidImageElements, getMermaidText, shouldRenderMermaid } from "..
import { DEBUGGING, debug } from "../utils/debugHelper"; import { DEBUGGING, debug } from "../utils/debugHelper";
import { Mutable } from "@zsviczian/excalidraw/types/excalidraw/utility-types"; import { Mutable } from "@zsviczian/excalidraw/types/excalidraw/utility-types";
import { updateElementIdsInScene } from "../utils/excalidrawSceneUtils"; import { updateElementIdsInScene } from "../utils/excalidrawSceneUtils";
import { getNewUniqueFilepath } from "../utils/fileUtils"; import { getNewUniqueFilepath, splitFolderAndFilename } from "../utils/fileUtils";
import { t } from "../lang/helpers"; import { t } from "../lang/helpers";
import { displayFontMessage } from "../utils/excalidrawViewUtils"; import { displayFontMessage } from "../utils/excalidrawViewUtils";
import { getPDFRect } from "../utils/PDFUtils"; import { getPDFRect } from "../utils/PDFUtils";
@@ -480,7 +480,7 @@ export class ExcalidrawData {
selectedElementIds: {[key:string]:boolean} = {}; //https://github.com/zsviczian/obsidian-excalidraw-plugin/issues/609 selectedElementIds: {[key:string]:boolean} = {}; //https://github.com/zsviczian/obsidian-excalidraw-plugin/issues/609
constructor( constructor(
private plugin: ExcalidrawPlugin, private plugin: ExcalidrawPlugin, private view?: ExcalidrawView,
) { ) {
this.app = this.plugin.app; this.app = this.plugin.app;
this.files = new Map<FileId, EmbeddedFile>(); this.files = new Map<FileId, EmbeddedFile>();
@@ -1546,13 +1546,23 @@ export class ExcalidrawData {
} }
} }
const x = await getAttachmentsFolderAndFilePath(this.app, this.file.path, fname); let hookFilepath:string;
const filepath = getNewUniqueFilepath(this.app.vault,fname,x.folder); const ea = this.view?.getHookServer();
if(ea?.onImageFilePathHook) {
hookFilepath = ea.onImageFilePathHook({
currentImageName: fname,
drawingFilePath: this.view?.file?.path,
})
}
/* let filepath:string;
const filepath = ( if(hookFilepath) {
await getAttachmentsFolderAndFilePath(this.app, this.file.path, fname) const {folderpath, filename} = splitFolderAndFilename(hookFilepath);
).filepath;*/ filepath = getNewUniqueFilepath(this.app.vault,filename,folderpath);
} else {
const x = await getAttachmentsFolderAndFilePath(this.app, this.file.path, fname);
filepath = getNewUniqueFilepath(this.app.vault,fname,x.folder);
}
const arrayBuffer = await getBinaryFileFromDataURL(dataURL); const arrayBuffer = await getBinaryFileFromDataURL(dataURL);
if(!arrayBuffer) return null; if(!arrayBuffer) return null;

View File

@@ -368,7 +368,7 @@ export default class ExcalidrawView extends TextFileView implements HoverParent{
constructor(leaf: WorkspaceLeaf, plugin: ExcalidrawPlugin) { constructor(leaf: WorkspaceLeaf, plugin: ExcalidrawPlugin) {
super(leaf); super(leaf);
this._plugin = plugin; this._plugin = plugin;
this.excalidrawData = new ExcalidrawData(plugin); this.excalidrawData = new ExcalidrawData(plugin, this);
this.canvasNodeFactory = new CanvasNodeFactory(this); this.canvasNodeFactory = new CanvasNodeFactory(this);
this.setHookServer(); this.setHookServer();
this.dropManager = new DropManager(this); this.dropManager = new DropManager(this);