mirror of
https://github.com/zsviczian/obsidian-excalidraw-plugin.git
synced 2025-08-06 05:46:28 +00:00
replace json.stringify with proper processing, fix small issues with Ephemral state, added worker (inactive)
This commit is contained in:
@@ -14,6 +14,29 @@ export const debug = (fn: Function, fnName: string, ...messages: unknown[]) => {
|
||||
console.log(fnName, ...messages);
|
||||
};
|
||||
|
||||
let timestamp: number[] = [];
|
||||
let tsOrigin: number = 0;
|
||||
|
||||
export function tsInit(msg: string) {
|
||||
tsOrigin = Date.now();
|
||||
timestamp = [tsOrigin, tsOrigin, tsOrigin, tsOrigin, tsOrigin]; // Initialize timestamps for L0 to L4
|
||||
console.log("0ms: " + msg);
|
||||
}
|
||||
|
||||
export function ts(msg: string, level: number) {
|
||||
if (level < 0 || level > 4) {
|
||||
console.error("Invalid level. Please use level 0, 1, 2, 3, or 4.");
|
||||
return;
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
const diff = now - timestamp[level];
|
||||
timestamp[level] = now;
|
||||
|
||||
const elapsedFromOrigin = now - tsOrigin;
|
||||
console.log(`L${level} (${elapsedFromOrigin}ms) ${diff}ms: ${msg}`);
|
||||
}
|
||||
|
||||
export class CustomMutationObserver {
|
||||
private originalCallback: MutationCallback;
|
||||
private observer: MutationObserver | null;
|
||||
|
||||
45
src/utils/ExcalidrawSceneUtils.ts
Normal file
45
src/utils/ExcalidrawSceneUtils.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { ExcalidrawArrowElement, ExcalidrawElement, ExcalidrawTextElement } from "@zsviczian/excalidraw/types/excalidraw/element/types";
|
||||
import { Mutable } from "@zsviczian/excalidraw/types/excalidraw/utility-types";
|
||||
|
||||
|
||||
export function updateElementIdsInScene(
|
||||
{elements: sceneElements}: {elements: Mutable<ExcalidrawElement>[]},
|
||||
elementToChange: Mutable<ExcalidrawElement>,
|
||||
newID: string
|
||||
) {
|
||||
if(elementToChange.type === "text") {
|
||||
const textElement = elementToChange as Mutable<ExcalidrawTextElement>;
|
||||
if(textElement.containerId) {
|
||||
const containerEl = sceneElements.find(el=>el.id === textElement.containerId) as unknown as Mutable<ExcalidrawElement>;
|
||||
containerEl.boundElements?.filter(x=>x.id === textElement.id).forEach( x => {
|
||||
(x.id as Mutable<string>) = newID;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if(elementToChange.boundElements?.length>0) {
|
||||
elementToChange.boundElements.forEach( binding => {
|
||||
const boundEl = sceneElements.find(el=>el.id === binding.id) as unknown as Mutable<ExcalidrawElement>;
|
||||
boundEl.boundElements?.filter(x=>x.id === elementToChange.id).forEach( x => {
|
||||
(x.id as Mutable<string>) = newID;
|
||||
});
|
||||
if(boundEl.type === "arrow") {
|
||||
const arrow = boundEl as Mutable<ExcalidrawArrowElement>;
|
||||
if(arrow.startBinding?.elementId === elementToChange.id) {
|
||||
arrow.startBinding.elementId = newID;
|
||||
}
|
||||
if(arrow.endBinding?.elementId === elementToChange.id) {
|
||||
arrow.endBinding.elementId = newID;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if(elementToChange.type === "frame") {
|
||||
sceneElements.filter(el=>el.frameId === elementToChange.id).forEach(x => {
|
||||
(x.frameId as Mutable<string>) = newID;
|
||||
});
|
||||
}
|
||||
|
||||
elementToChange.id = newID;
|
||||
}
|
||||
@@ -529,11 +529,29 @@ export function getLinkParts (fname: string, file?: TFile): LinkParts {
|
||||
};
|
||||
|
||||
export function compress (data: string): string {
|
||||
return LZString.compressToBase64(data).replace(/(.{256})/g, "$1\n\n");
|
||||
const compressed = LZString.compressToBase64(data);
|
||||
|
||||
let result = '';
|
||||
const chunkSize = 256;
|
||||
for (let i = 0; i < compressed.length; i += chunkSize) {
|
||||
result += compressed.slice(i, i + chunkSize) + '\n\n';
|
||||
}
|
||||
|
||||
return result.trim();
|
||||
};
|
||||
|
||||
export function decompress (data: string): string {
|
||||
return LZString.decompressFromBase64(data.replaceAll("\n", "").replaceAll("\r", ""));
|
||||
let cleanedData = '';
|
||||
const length = data.length;
|
||||
|
||||
for (let i = 0; i < length; i++) {
|
||||
const char = data[i];
|
||||
if (char !== '\n' && char !== '\r') {
|
||||
cleanedData += char;
|
||||
}
|
||||
}
|
||||
|
||||
return LZString.decompressFromBase64(cleanedData);
|
||||
};
|
||||
|
||||
export function isMaskFile (
|
||||
|
||||
Reference in New Issue
Block a user