diff --git a/manifest.json b/manifest.json index 07da2ec..444a70b 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-excalidraw-plugin", "name": "Excalidraw", - "version": "1.3.18", + "version": "1.3.19", "minAppVersion": "0.12.0", "description": "An Obsidian plugin to edit and view Excalidraw drawings", "author": "Zsolt Viczian", diff --git a/package.json b/package.json index 2c2b941..8f9ba7b 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "author": "", "license": "MIT", "dependencies": { - "@zsviczian/excalidraw": "0.9.0-obsidian-12", + "@zsviczian/excalidraw": "0.9.0-obsidian-13", "monkey-around": "^2.2.0", "react": "^17.0.2", "react-dom": "^17.0.2", diff --git a/src/ExcalidrawData.ts b/src/ExcalidrawData.ts index 84941a4..d87196c 100644 --- a/src/ExcalidrawData.ts +++ b/src/ExcalidrawData.ts @@ -25,7 +25,10 @@ declare module "obsidian" { export const REGEX_LINK = { //![[link|alias]] [alias](link){num} // 1 2 3 4 5 6 7 8 9 - EXPR: /(!)?(\[\[([^|\]]+)\|?(.+)?]]|\[(.*)\]\((.*)\))(\{(\d+)\})?/g, + EXPR: /(!)?(\[\[([^|\]]+)\|?([^\]]+)?]]|\[([^\]]*)]\(([^)]*)\))(\{(\d+)\})?/g, //https://github.com/zsviczian/obsidian-excalidraw-plugin/issues/187 + getRes: (text:string):IterableIterator => { + return text.matchAll(REGEX_LINK.EXPR); + }, isTransclusion: (parts: IteratorResult):boolean => { return parts.value[1] ? true:false; }, @@ -48,7 +51,7 @@ export const REGEX_LINK = { export const REG_LINKINDEX_HYPERLINK = /^\w+:\/\//; -const DRAWING_REG = /\n%%\n# Drawing\n(```json\n)(.*)\n```%%/gm; +const DRAWING_REG = /\n%%\n# Drawing\n[^`]*(```json\n)(.*)\n[^`]*```[^%]*(%%)?/gm; //https://github.com/zsviczian/obsidian-excalidraw-plugin/issues/182 const DRAWING_REG_FALLBACK = /\n# Drawing\n(```json\n)?(.*)(```)?(%%)?/gm; export function getJSON(data:string):[string,number] { let res = data.matchAll(DRAWING_REG); @@ -350,7 +353,7 @@ export class ExcalidrawData { private async parse(text:string):Promise{ let outString = ""; let position = 0; - const res = text.matchAll(REGEX_LINK.EXPR); + const res = REGEX_LINK.getRes(text); let linkIcon = false; let urlIcon = false; let parts; @@ -390,7 +393,7 @@ export class ExcalidrawData { */ private quickParse(text:string):string { const hasTransclusion = (text:string):boolean => { - const res = text.matchAll(REGEX_LINK.EXPR); + const res = REGEX_LINK.getRes(text); let parts; while(!(parts=res.next()).done) { if (REGEX_LINK.isTransclusion(parts)) return true; @@ -401,7 +404,7 @@ export class ExcalidrawData { let outString = ""; let position = 0; - const res = text.matchAll(REGEX_LINK.EXPR); + const res = REGEX_LINK.getRes(text); let linkIcon = false; let urlIcon = false; let parts; diff --git a/src/ExcalidrawView.ts b/src/ExcalidrawView.ts index 8247d76..a7d2314 100644 --- a/src/ExcalidrawView.ts +++ b/src/ExcalidrawView.ts @@ -210,12 +210,13 @@ export default class ExcalidrawView extends TextFileView { new Notice(t("LINK_BUTTON_CLICK_NO_TEXT"),20000); return; } + text = text.replaceAll("\n",""); //https://github.com/zsviczian/obsidian-excalidraw-plugin/issues/187 if(text.match(REG_LINKINDEX_HYPERLINK)) { window.open(text,"_blank"); return; } - const parts = text.matchAll(REGEX_LINK.EXPR).next(); + const parts = REGEX_LINK.getRes(text).next(); if(!parts.value) { const tags = text.matchAll(/#([\p{Letter}\p{Emoji_Presentation}\p{Number}\/_-]+)/ug).next(); if(!tags.value || tags.value.length<2) { @@ -227,7 +228,6 @@ export default class ExcalidrawView extends TextFileView { //@ts-ignore search[0].view.setQuery("tag:"+tags.value[1]); this.app.workspace.revealLeaf(search[0]); - //if(this.gotoFullscreen.style.display=="none") this.toggleFullscreen(); if(document.fullscreenElement === this.contentEl) { document.exitFullscreen(); this.zoomToFit(); @@ -235,7 +235,7 @@ export default class ExcalidrawView extends TextFileView { return; } - text = REGEX_LINK.getLink(parts); //parts.value[2] ? parts.value[2]:parts.value[6]; + text = REGEX_LINK.getLink(parts); if(text.match(REG_LINKINDEX_HYPERLINK)) { window.open(text,"_blank"); @@ -265,8 +265,8 @@ export default class ExcalidrawView extends TextFileView { this.zoomToFit(); } const leaf = ev.shiftKey ? getNewOrAdjacentLeaf(this.plugin,view.leaf) : view.leaf; - const eState = lineNum ? {eState: {line: lineNum-1}} : {}; - leaf.openFile(file,eState); + view.app.workspace.setActiveLeaf(leaf); + leaf.view.app.workspace.openLinkText(text,view.file.path); } catch (e) { new Notice(e,4000); } @@ -721,7 +721,7 @@ export default class ExcalidrawView extends TextFileView { : e.text; if(!text) return false; if(text.match(REG_LINKINDEX_HYPERLINK)) return true; - const parts = text.matchAll(REGEX_LINK.EXPR).next(); + const parts = REGEX_LINK.getRes(text).next(); if(!parts.value) return false; return true; }); @@ -789,7 +789,7 @@ export default class ExcalidrawView extends TextFileView { if(!text) return; if(text.match(REG_LINKINDEX_HYPERLINK)) return; - const parts = text.matchAll(REGEX_LINK.EXPR).next(); + const parts = REGEX_LINK.getRes(text).next(); if(!parts.value) return; let linktext = REGEX_LINK.getLink(parts); //parts.value[2] ? parts.value[2]:parts.value[6]; diff --git a/src/Utils.ts b/src/Utils.ts index 85c50ad..1047e8d 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -170,6 +170,7 @@ export const getNewOrAdjacentLeaf = (plugin: ExcalidrawPlugin, leaf: WorkspaceLe if(!leafToUse){leafToUse = plugin.app.workspace.getAdjacentLeafInDirection(leaf, "left");} if(!leafToUse){leafToUse = plugin.app.workspace.getAdjacentLeafInDirection(leaf, "bottom");} if(!leafToUse){leafToUse = plugin.app.workspace.getAdjacentLeafInDirection(leaf, "top");} + if(!leafToUse){leafToUse = plugin.app.workspace.createLeafBySplit(leaf);} return leafToUse; } return plugin.app.workspace.createLeafBySplit(leaf); diff --git a/src/constants.ts b/src/constants.ts index 0b0efd1..8eb0344 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -13,6 +13,7 @@ export const MAX_COLORS = 5; export const COLOR_FREQ = 6; export const RERENDER_EVENT = "excalidraw-embed-rerender"; export const BLANK_DRAWING = '{"type":"excalidraw","version":2,"source":"https://excalidraw.com","elements":[],"appState":{"gridSize":null,"viewBackgroundColor":"#ffffff"}}'; +export const DARK_BLANK_DRAWING = '{"type":"excalidraw","version":2,"source":"https://excalidraw.com","elements":[],"appState":{"theme":"dark","gridSize":null,"viewBackgroundColor":"#ffffff"}}'; export const FRONTMATTER = ["---","",`${FRONTMATTER_KEY}: parsed`,"","---", "==⚠ Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ⚠==", "",""].join("\n"); export const EMPTY_MESSAGE = "Hit enter to create a new drawing"; export const TEXT_DISPLAY_PARSED_ICON_NAME = "quote-glyph"; diff --git a/src/lang/locale/en.ts b/src/lang/locale/en.ts index 5379e29..f35012c 100644 --- a/src/lang/locale/en.ts +++ b/src/lang/locale/en.ts @@ -70,6 +70,9 @@ export default { FILENAME_DATE_NAME: "Filename date", FILENAME_DATE_DESC: "The second part of the filename", DISPLAY_HEAD: "Display", + MATCH_THEME_NAME: "New drawing to match Obsidian theme", + MATCH_THEME_DESC: "If theme is dark, new drawing will be created in dark mode. This does not apply when you use a template for new drawings. " + + "Also this will not effect when you open an existing drawing. Those will follow the theme of the template/drawing respectively.", ZOOM_TO_FIT_NAME: "Zoom to fit on view resize", ZOOM_TO_FIT_DESC: "Zoom to fit drawing when the pane is resized", LINKS_HEAD: "Links and transclusion", diff --git a/src/main.ts b/src/main.ts index 2232602..cb9a1d1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -31,7 +31,8 @@ import { FRONTMATTER_KEY, FRONTMATTER, JSON_parse, - nanoid + nanoid, + DARK_BLANK_DRAWING } from "./constants"; import ExcalidrawView, {ExportSettings, TextMode} from "./ExcalidrawView"; import {getJSON} from "./ExcalidrawData"; @@ -1101,9 +1102,10 @@ export default class ExcalidrawPlugin extends Plugin { } } if (this.settings.compatibilityMode) { - return BLANK_DRAWING; + return this.settings.matchTheme && document.body.classList.contains("theme-dark") ? DARK_BLANK_DRAWING : BLANK_DRAWING; } - return FRONTMATTER + '\n' + this.getMarkdownDrawingSection(BLANK_DRAWING); + const blank = this.settings.matchTheme && document.body.classList.contains("theme-dark") ? DARK_BLANK_DRAWING : BLANK_DRAWING; + return FRONTMATTER + '\n' + this.getMarkdownDrawingSection(blank); } public getMarkdownDrawingSection(jsonString: string) { diff --git a/src/settings.ts b/src/settings.ts index 6f02246..c01c5eb 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -17,6 +17,7 @@ export interface ExcalidrawSettings { drawingFilenameDateTime: string, displaySVGInPreview: boolean, width: string, + matchTheme: boolean, zoomToFitOnResize: boolean, openInAdjacentPane: boolean, showLinkBrackets: boolean, @@ -50,6 +51,7 @@ export const DEFAULT_SETTINGS: ExcalidrawSettings = { drawingFilenameDateTime: 'YYYY-MM-DD HH.mm.ss', displaySVGInPreview: true, width: '400', + matchTheme: false, zoomToFitOnResize: true, linkPrefix: "📍", urlPrefix: "🌐", @@ -191,6 +193,17 @@ export class ExcalidrawSettingTab extends PluginSettingTab { })); this.containerEl.createEl('h1', {text: t("DISPLAY_HEAD")}); + + new Setting(containerEl) + .setName(t("MATCH_THEME_NAME")) + .setDesc(t("MATCH_THEME_DESC")) + .addToggle(toggle => toggle + .setValue(this.plugin.settings.matchTheme) + .onChange(async (value) => { + this.plugin.settings.matchTheme = value; + this.applySettingsUpdate(); + })); + new Setting(containerEl) .setName(t("ZOOM_TO_FIT_NAME")) .setDesc(t("ZOOM_TO_FIT_DESC")) diff --git a/versions.json b/versions.json index 7e8f51d..58d72b8 100644 --- a/versions.json +++ b/versions.json @@ -1,3 +1,3 @@ { - "1.3.18": "0.11.13" + "1.3.19": "0.11.13" } diff --git a/yarn.lock b/yarn.lock index 751e4d4..0d18634 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1062,10 +1062,10 @@ dependencies: "@types/estree" "*" -"@zsviczian/excalidraw@0.9.0-obsidian-12": - "integrity" "sha512-b3TgSXfvCAY+1pbvUfPoybijvaye35rqlGc5ZrNUdvEfoNwfTRuIa7O9eWqHGFH67Q959MYa8kXWwzghwWw5PA==" - "resolved" "https://registry.npmjs.org/@zsviczian/excalidraw/-/excalidraw-0.9.0-obsidian-12.tgz" - "version" "0.9.0-obsidian-12" +"@zsviczian/excalidraw@0.9.0-obsidian-13": + "integrity" "sha512-oHXNHsdtbFsKJDeibsJi6DFeqTgb0OrZjE0W1ZgE3VN3CeqC0Ad9okzHMXQgPicpw/XFx1IOtCqZtpQ2Cch/Sw==" + "resolved" "https://registry.npmjs.org/@zsviczian/excalidraw/-/excalidraw-0.9.0-obsidian-13.tgz" + "version" "0.9.0-obsidian-13" "abab@^1.0.3": "integrity" "sha1-X6rZwsB/YN12dw9xzwJbYqY8/U4="