|
125 | 125 | * @returns {string} - The new filepath for the image including full vault path and extension. |
126 | 126 | * |
127 | 127 | * Example usage: |
128 | | - * ``` |
129 | 128 | * onImageFilePathHook: (data) => { |
130 | 129 | * const { currentImageName, drawingFilePath } = data; |
131 | 130 | * const ext = currentImageName.split('.').pop(); |
132 | 131 | * // Generate a new filepath based on the drawing file name and other criteria |
133 | 132 | * return `${drawingFileName} - ${currentImageName || 'image'}.${ext}`; |
134 | 133 | * } |
135 | | - * ``` |
136 | | - * onImageFilePathHook: (data: { |
| 134 | + * |
| 135 | + * Signiture: |
| 136 | + * onImageFilePathHook: (data: { |
137 | 137 | * currentImageName: string; // Excalidraw generated name of the image, or the name received from the file system. |
138 | 138 | * drawingFilePath: string; // The full filepath of the Excalidraw file where the image is being used. |
139 | 139 | * }) => string = null; |
140 | 140 | */ |
141 | | -//ea.onImageFilePathHook = (data) => {}; |
| 141 | +// ea.onImageFilePathHook = (data) => { console.log(data); }; |
| 142 | +
|
| 143 | +/** |
| 144 | + * If set, this callback is triggered when the Excalidraw image is being exported to |
| 145 | + * .svg, .png, or .excalidraw. |
| 146 | + * You can use this callback to customize the naming and path of the images. This allows |
| 147 | + * you to place images into an assets folder. |
| 148 | + * |
| 149 | + * If the function returns null or undefined, the normal Excalidraw operation will continue |
| 150 | + * with the currentImageName and in the same folder as the Excalidraw file |
| 151 | + * If a filepath is returned, that will be used. Include the full Vault filepath and filename |
| 152 | + * with the file extension. |
| 153 | + * !!!! If an image already exists on the path, that will be overwritten. When returning |
| 154 | + * your own image path, you must take care of unique filenames (if that is a requirement) !!!! |
| 155 | + * The current image name is the name generated by Excalidraw: |
| 156 | + * - my-drawing.png |
| 157 | + * - my-drawing.svg |
| 158 | + * - my-drawing.excalidraw |
| 159 | + * - my-drawing.dark.svg |
| 160 | + * - my-drawing.light.svg |
| 161 | + * - my-drawing.dark.png |
| 162 | + * - my-drawing.light.png |
| 163 | + * |
| 164 | + * @param data - An object containing the following properties: |
| 165 | + * @property {string} exportFilepath - Default export filepath for the image. |
| 166 | + * @property {string} excalidrawFile - TFile: The Excalidraw file being exported. |
| 167 | + * @property {string} exportExtension - The file extension of the export (e.g., .dark.svg, .png, .excalidraw). |
| 168 | + * @property {string} oldExcalidrawPath - If action === "move" The old path of the Excalidraw file, else undefined |
| 169 | + * @property {string} action - The action being performed: |
| 170 | + * "export" | "move" | "delete" |
| 171 | + * move and delete reference the change to the Excalidraw file. |
| 172 | + * |
| 173 | + * @returns {string} - The new filepath for the image including full vault path and extension. |
| 174 | + * |
| 175 | + * action === "move" || action === "delete" is only possible if "keep in sync" is enabled |
| 176 | + * in plugin export settings |
| 177 | + * |
| 178 | + * Example usage: |
| 179 | + * onImageFilePathHook: (data) => { |
| 180 | + * const { currentImageName, drawingFilePath, frontmatter } = data; |
| 181 | + * // Generate a new filepath based on the drawing file name and other criteria |
| 182 | + * const ext = currentImageName.split('.').pop(); |
| 183 | + * if(frontmatter && frontmatter["my-custom-field"]) { |
| 184 | + * } |
| 185 | + * return `${drawingFileName} - ${currentImageName || 'image'}.${ext}`; |
| 186 | + * } |
| 187 | + * |
| 188 | +*/ |
| 189 | +/*ea.onImageExportPathHook = (data) => { |
| 190 | + //debugger; //remove comment to debug using Developer Console |
| 191 | + |
| 192 | + let {excalidrawFile, exportFilepath, exportExtension, oldExcalidrawPath, action} = data; |
| 193 | + const frontmatter = app.metadataCache.getFileCache(excalidrawFile)?.frontmatter; |
| 194 | + //console.log(data, frontmatter); |
| 195 | + |
| 196 | + const excalidrawFilename = action === "move" |
| 197 | + ? ea.splitFolderAndFilename(excalidrawFile.name).filename |
| 198 | + : excalidrawFile.name |
| 199 | +
|
| 200 | + if(excalidrawFilename.match(/^icon - /i)) { |
| 201 | + const {folderpath, filename, basename, extension} = ea.splitFolderAndFilename(exportFilepath); |
| 202 | + exportFilepath = "assets/icons/" + filename; |
| 203 | + return exportFilepath; |
| 204 | + } |
| 205 | + |
| 206 | + if(excalidrawFilename.match(/^stickfigure - /i)) { |
| 207 | + const {folderpath, filename, basename, extension} = ea.splitFolderAndFilename(exportFilepath); |
| 208 | + exportFilepath = "assets/stickfigures/" + filename; |
| 209 | + return exportFilepath; |
| 210 | + } |
| 211 | + |
| 212 | + if(excalidrawFilename.match(/^logo - /i)) { |
| 213 | + const {folderpath, filename, basename, extension} = ea.splitFolderAndFilename(exportFilepath); |
| 214 | + exportFilepath = "assets/logos/" + filename; |
| 215 | + return exportFilepath; |
| 216 | + } |
| 217 | +
|
| 218 | + // !!!! frontmatter will be undefined when action === "delete" |
| 219 | + // this means if you base your logic on frontmatter properties, then |
| 220 | + // plugin settings keep files in sync will break for those files when |
| 221 | + // deleting the Excalidraw file. The images will not be deleted, or worst |
| 222 | + // your logic might result in deleting other files. This hook gives you |
| 223 | + // powerful control, but the hook function logic requires careful testing |
| 224 | + // on your part. |
| 225 | + //if(frontmatter && frontmatter["is-asset"]) { //custom frontmatter property |
| 226 | + exportFilepath = ea.obsidian.normalizePath("assets/" + exportFilepath); |
| 227 | + return exportFilepath; |
| 228 | + //} |
| 229 | +
|
| 230 | + return exportFilepath; |
| 231 | +};*/ |
| 232 | +
|
| 233 | +/** |
| 234 | + * Excalidraw supports auto-export of Excalidraw files to .png, .svg, and .excalidraw formats. |
| 235 | + * |
| 236 | + * Auto-export of Excalidraw files can be controlled at multiple levels. |
| 237 | + * 1) In plugin settings where you can set up default auto-export applicable to all your Excalidraw files. |
| 238 | + * 2) However, if you do not want to auto-export every file, you can also control auto-export |
| 239 | + * at the file level using the 'excalidraw-autoexport' frontmatter property. |
| 240 | + * 3) This hook gives you an additional layer of control over the auto-export process. |
| 241 | + * |
| 242 | + * This hook is triggered when an Excalidraw file is being saved. |
| 243 | + * |
| 244 | + * interface AutoexportConfig { |
| 245 | + * png: boolean; // Whether to auto-export to PNG |
| 246 | + * svg: boolean; // Whether to auto-export to SVG |
| 247 | + * excalidraw: boolean; // Whether to auto-export to Excalidraw format |
| 248 | + * theme: "light" | "dark" | "both"; // The theme to use for the export |
| 249 | + * } |
| 250 | + * |
| 251 | + * @param {Object} data - The data for the hook. |
| 252 | + * @param {AutoexportConfig} data.autoexportConfig - The current autoexport configuration. |
| 253 | + * @param {TFile} data.excalidrawFile - The Excalidraw file being auto-exported. |
| 254 | + * @returns {AutoexportConfig | null} - Return a modified AutoexportConfig to override the export behavior, or null to use the default. |
| 255 | +*/ |
| 256 | +/*ea.onTriggerAutoexportHook = (data) => { |
| 257 | + let {autoexportConfig, excalidrawFile} = data; |
| 258 | + const frontmatter = app.metadataCache.getFileCache(excalidrawFile)?.frontmatter; |
| 259 | + //console.log(data, frontmatter); |
| 260 | + //logic based on filepath and frontmatter |
| 261 | + if(excalidrawFile.name.match(/^(?:icon|stickfigure|logo) - /i)) { |
| 262 | + autoexportConfig.theme = "light"; |
| 263 | + autoexportConfig.svg = true; |
| 264 | + autoexportConfig.png = false; |
| 265 | + autoexportConfig.excalidraw = false; |
| 266 | + return autoexportConfig; |
| 267 | + } |
| 268 | + return autoexportConfig; |
| 269 | +};*/ |
142 | 270 |
|
143 | 271 | /** |
144 | 272 | * If set, this callback is triggered whenever the active canvas color changes |
|
147 | 275 | * view: ExcalidrawView, //the excalidraw view |
148 | 276 | * color: string, |
149 | 277 | * ) => void = null; |
150 | | - */ |
151 | | -//ea.onCanvasColorChangeHook = (ea, view, color) => {}; |
| 278 | +*/ |
| 279 | +//ea.onCanvasColorChangeHook = (ea, view, color) => {}; |
0 commit comments