import { customAlphabet } from "nanoid"; import { DeviceType } from "../types"; import { ExcalidrawLib } from "../ExcalidrawLib"; import { moment } from "obsidian"; //This is only for backward compatibility because an early version of obsidian included an encoding to avoid fantom links from littering Obsidian graph view declare const PLUGIN_VERSION:string; export const ERROR_IFRAME_CONVERSION_CANCELED = "iframe conversion canceled"; declare const excalidrawLib: typeof ExcalidrawLib; export const LOCALE = moment.locale(); export const obsidianToExcalidrawMap: { [key: string]: string } = { 'en': 'en-US', 'af': 'af-ZA', // Assuming South Africa for Afrikaans 'am': 'am-ET', // Assuming Ethiopia for Amharic 'ar': 'ar-SA', 'eu': 'eu-ES', 'be': 'be-BY', // Assuming Belarus for Belarusian 'bg': 'bg-BG', 'bn': 'bn-BD', // Assuming Bangladesh for Bengali 'ca': 'ca-ES', 'cs': 'cs-CZ', 'da': 'da-DK', // Assuming Denmark for Danish 'de': 'de-DE', 'el': 'el-GR', 'eo': 'eo-EO', // Esperanto doesn't have a country 'es': 'es-ES', 'fa': 'fa-IR', 'fi-fi': 'fi-FI', 'fr': 'fr-FR', 'gl': 'gl-ES', 'he': 'he-IL', 'hi': 'hi-IN', 'hu': 'hu-HU', 'id': 'id-ID', 'it': 'it-IT', 'ja': 'ja-JP', 'ko': 'ko-KR', 'lv': 'lv-LV', 'ml': 'ml-IN', // Assuming India for Malayalam 'ms': 'ms-MY', // Assuming Malaysia for Malay 'nl': 'nl-NL', 'no': 'nb-NO', // Using Norwegian Bokmål for Norwegian 'oc': 'oc-FR', // Assuming France for Occitan 'pl': 'pl-PL', 'pt': 'pt-PT', 'pt-BR': 'pt-BR', 'ro': 'ro-RO', 'ru': 'ru-RU', 'sr': 'sr-RS', // Assuming Serbia for Serbian 'se': 'sv-SE', // Assuming Swedish for 'se' 'sk': 'sk-SK', 'sq': 'sq-AL', // Assuming Albania for Albanian 'ta': 'ta-IN', // Assuming India for Tamil 'te': 'te-IN', // Assuming India for Telugu 'th': 'th-TH', 'tr': 'tr-TR', 'uk': 'uk-UA', 'ur': 'ur-PK', // Assuming Pakistan for Urdu 'vi': 'vi-VN', 'zh': 'zh-CN', 'zh-TW': 'zh-TW', }; export const { sceneCoordsToViewportCoords, viewportCoordsToSceneCoords, determineFocusDistance, intersectElementWithLine, getCommonBoundingBox, getMaximumGroups, measureText, getDefaultLineHeight, wrapText, getFontString, getBoundTextMaxWidth, exportToSvg, exportToBlob, mutateElement, restore, mermaidToExcalidraw, } = excalidrawLib; export function JSON_parse(x: string): any { return JSON.parse(x.replaceAll("[", "[")); } export const isDarwin = /Mac|iPod|iPhone|iPad/.test(window.navigator.platform); export const DEVICE: DeviceType = { isDesktop: !document.body.hasClass("is-tablet") && !document.body.hasClass("is-mobile"), isPhone: document.body.hasClass("is-phone"), isTablet: document.body.hasClass("is-tablet"), isMobile: document.body.hasClass("is-mobile"), //running Obsidian Mobile, need to also check isTablet isLinux: document.body.hasClass("mod-linux") && ! document.body.hasClass("is-android"), isMacOS: document.body.hasClass("mod-macos") && ! document.body.hasClass("is-ios"), isWindows: document.body.hasClass("mod-windows"), isIOS: document.body.hasClass("is-ios"), isAndroid: document.body.hasClass("is-android") }; export const ROOTELEMENTSIZE = (() => { const tempElement = document.createElement('div'); tempElement.style.fontSize = '1rem'; tempElement.style.display = 'none'; // Hide the element document.body.appendChild(tempElement); const computedStyle = getComputedStyle(tempElement); const pixelSize = parseFloat(computedStyle.fontSize); document.body.removeChild(tempElement); return pixelSize; })(); export const nanoid = customAlphabet( "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 8, ); export const KEYCODE = { ESC: 27, }; export const ROUNDNESS = { //should at one point publish @zsviczian/excalidraw/types/constants LEGACY: 1, PROPORTIONAL_RADIUS: 2, ADAPTIVE_RADIUS: 3, } as const; export const THEME_FILTER = "invert(100%) hue-rotate(180deg) saturate(1.25)"; export const GITHUB_RELEASES = "https://github.com/zsviczian/obsidian-excalidraw-plugin/releases/tag/"; export const URLFETCHTIMEOUT = 3000; export const PLUGIN_ID = "obsidian-excalidraw-plugin"; export const SCRIPT_INSTALL_CODEBLOCK = "excalidraw-script-install"; export const SCRIPT_INSTALL_FOLDER = "Downloaded"; export const fileid = customAlphabet("1234567890abcdef", 40); export const REG_LINKINDEX_INVALIDCHARS = /[<>:"\\|?*#]/g; //taken from Obsidian source code export const REG_SECTION_REF_CLEAN = /([:#|^\\\r\n]|%%|\[\[|]])/g; export const REG_BLOCK_REF_CLEAN = /[!"#$%&()*+,.:;<=>?@^`{|}~\/\[\]\\\r\n]/g; // /[!"#$%&()*+,.:;<=>?@^`{|}~\/\[\]\\]/g; // https://discord.com/channels/686053708261228577/989603365606531104/1000128926619816048 // /\+|\/|~|=|%|\(|\)|{|}|,|&|\.|\$|!|\?|;|\[|]|\^|#|\*|<|>|&|@|\||\\|"|:|\s/g; export const IMAGE_TYPES = ["jpeg", "jpg", "png", "gif", "svg", "webp", "bmp", "ico", "jtif", "tif"]; export const ANIMATED_IMAGE_TYPES = ["gif", "webp", "apng", "svg"]; export const EXPORT_TYPES = ["svg", "dark.svg", "light.svg", "png", "dark.png", "light.png"]; export const MAX_IMAGE_SIZE = 500; export const FRONTMATTER_KEYS:{[key:string]: {name: string, type: string, depricated?:boolean}} = { "plugin": {name: "excalidraw-plugin", type: "text"}, "export-transparent": {name: "excalidraw-export-transparent", type: "checkbox"}, "mask": {name: "excalidraw-mask", type: "checkbox"}, "export-dark": {name: "excalidraw-export-dark", type: "checkbox"}, "export-svgpadding": {name: "excalidraw-export-svgpadding", type: "number", depricated: true}, "export-padding": {name: "excalidraw-export-padding", type: "number"}, "export-pngscale": {name: "excalidraw-export-pngscale", type: "number"}, "link-prefix": {name: "excalidraw-link-prefix", type: "text"}, "url-prefix": {name: "excalidraw-url-prefix", type: "text"}, "link-brackets": {name: "excalidraw-link-brackets", type: "checkbox"}, "onload-script": {name: "excalidraw-onload-script", type: "text"}, "linkbutton-opacity": {name: "excalidraw-linkbutton-opacity", type: "number"}, "default-mode": {name: "excalidraw-default-mode", type: "text"}, "font": {name: "excalidraw-font", type: "text"}, "font-color": {name: "excalidraw-font-color", type: "text"}, "border-color": {name: "excalidraw-border-color", type: "text"}, "md-css": {name: "excalidraw-css", type: "text"}, "autoexport": {name: "excalidraw-autoexport", type: "checkbox"}, "iframe-theme": {name: "excalidraw-iframe-theme", type: "text"}, }; export const EMBEDDABLE_THEME_FRONTMATTER_VALUES = ["light", "dark", "auto", "dafault"]; export const VIEW_TYPE_EXCALIDRAW = "excalidraw"; export const ICON_NAME = "excalidraw-icon"; 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":"${GITHUB_RELEASES+PLUGIN_VERSION}","elements":[],"appState":{"gridSize":null,"viewBackgroundColor":"#ffffff"}}`; export const DARK_BLANK_DRAWING = `{"type":"excalidraw","version":2,"source":"${GITHUB_RELEASES+PLUGIN_VERSION}","elements":[],"appState":{"theme":"dark","gridSize":null,"viewBackgroundColor":"#ffffff"}}`; export const FRONTMATTER = [ "---", "", `${FRONTMATTER_KEYS["plugin"].name}: parsed`, "tags: [excalidraw]", "", "---", "==⚠ 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"; export const TEXT_DISPLAY_RAW_ICON_NAME = "presentation"; /*export const FULLSCREEN_ICON_NAME = "fullscreen"; export const EXIT_FULLSCREEN_ICON_NAME = "exit-fullscreen";*/ export const SCRIPTENGINE_ICON_NAME = "ScriptEngine"; export const KEYBOARD_EVENT_TYPES = [ "keydown", "keyup", "keypress" ]; export const EXTENDED_EVENT_TYPES = [ /* "pointerdown", "pointerup", "pointermove", "mousedown", "mouseup", "mousemove", "mouseover", "mouseout", "mouseenter", "mouseleave", "dblclick", "drag", "dragend", "dragenter", "dragexit", "dragleave", "dragover", "dragstart", "drop",*/ "copy", "cut", "paste", /*"wheel", "touchstart", "touchend", "touchmove",*/ ]; //export const TWITTER_REG = /^(?:http(?:s)?:\/\/)?(?:(?:w){3}.)?twitter.com/; export const COLOR_NAMES = new Map(); COLOR_NAMES.set("aliceblue", "#f0f8ff"); COLOR_NAMES.set("antiquewhite", "#faebd7"); COLOR_NAMES.set("aqua", "#00ffff"); COLOR_NAMES.set("aquamarine", "#7fffd4"); COLOR_NAMES.set("azure", "#f0ffff"); COLOR_NAMES.set("beige", "#f5f5dc"); COLOR_NAMES.set("bisque", "#ffe4c4"); COLOR_NAMES.set("black", "#000000"); COLOR_NAMES.set("blanchedalmond", "#ffebcd"); COLOR_NAMES.set("blue", "#0000ff"); COLOR_NAMES.set("blueviolet", "#8a2be2"); COLOR_NAMES.set("brown", "#a52a2a"); COLOR_NAMES.set("burlywood", "#deb887"); COLOR_NAMES.set("cadetblue", "#5f9ea0"); COLOR_NAMES.set("chartreuse", "#7fff00"); COLOR_NAMES.set("chocolate", "#d2691e"); COLOR_NAMES.set("coral", "#ff7f50"); COLOR_NAMES.set("cornflowerblue", "#6495ed"); COLOR_NAMES.set("cornsilk", "#fff8dc"); COLOR_NAMES.set("crimson", "#dc143c"); COLOR_NAMES.set("cyan", "#00ffff"); COLOR_NAMES.set("darkblue", "#00008b"); COLOR_NAMES.set("darkcyan", "#008b8b"); COLOR_NAMES.set("darkgoldenrod", "#b8860b"); COLOR_NAMES.set("darkgray", "#a9a9a9"); COLOR_NAMES.set("darkgreen", "#006400"); COLOR_NAMES.set("darkkhaki", "#bdb76b"); COLOR_NAMES.set("darkmagenta", "#8b008b"); COLOR_NAMES.set("darkolivegreen", "#556b2f"); COLOR_NAMES.set("darkorange", "#ff8c00"); COLOR_NAMES.set("darkorchid", "#9932cc"); COLOR_NAMES.set("darkred", "#8b0000"); COLOR_NAMES.set("darksalmon", "#e9967a"); COLOR_NAMES.set("darkseagreen", "#8fbc8f"); COLOR_NAMES.set("darkslateblue", "#483d8b"); COLOR_NAMES.set("darkslategray", "#2f4f4f"); COLOR_NAMES.set("darkturquoise", "#00ced1"); COLOR_NAMES.set("darkviolet", "#9400d3"); COLOR_NAMES.set("deeppink", "#ff1493"); COLOR_NAMES.set("deepskyblue", "#00bfff"); COLOR_NAMES.set("dimgray", "#696969"); COLOR_NAMES.set("dodgerblue", "#1e90ff"); COLOR_NAMES.set("firebrick", "#b22222"); COLOR_NAMES.set("floralwhite", "#fffaf0"); COLOR_NAMES.set("forestgreen", "#228b22"); COLOR_NAMES.set("fuchsia", "#ff00ff"); COLOR_NAMES.set("gainsboro", "#dcdcdc"); COLOR_NAMES.set("ghostwhite", "#f8f8ff"); COLOR_NAMES.set("gold", "#ffd700"); COLOR_NAMES.set("goldenrod", "#daa520"); COLOR_NAMES.set("gray", "#808080"); COLOR_NAMES.set("green", "#008000"); COLOR_NAMES.set("greenyellow", "#adff2f"); COLOR_NAMES.set("honeydew", "#f0fff0"); COLOR_NAMES.set("hotpink", "#ff69b4"); COLOR_NAMES.set("indianred", "#cd5c5c"); COLOR_NAMES.set("indigo", "#4b0082"); COLOR_NAMES.set("ivory", "#fffff0"); COLOR_NAMES.set("khaki", "#f0e68c"); COLOR_NAMES.set("lavender", "#e6e6fa"); COLOR_NAMES.set("lavenderblush", "#fff0f5"); COLOR_NAMES.set("lawngreen", "#7cfc00"); COLOR_NAMES.set("lemonchiffon", "#fffacd"); COLOR_NAMES.set("lightblue", "#add8e6"); COLOR_NAMES.set("lightcoral", "#f08080"); COLOR_NAMES.set("lightcyan", "#e0ffff"); COLOR_NAMES.set("lightgoldenrodyellow", "#fafad2"); COLOR_NAMES.set("lightgrey", "#d3d3d3"); COLOR_NAMES.set("lightgreen", "#90ee90"); COLOR_NAMES.set("lightpink", "#ffb6c1"); COLOR_NAMES.set("lightsalmon", "#ffa07a"); COLOR_NAMES.set("lightseagreen", "#20b2aa"); COLOR_NAMES.set("lightskyblue", "#87cefa"); COLOR_NAMES.set("lightslategray", "#778899"); COLOR_NAMES.set("lightsteelblue", "#b0c4de"); COLOR_NAMES.set("lightyellow", "#ffffe0"); COLOR_NAMES.set("lime", "#00ff00"); COLOR_NAMES.set("limegreen", "#32cd32"); COLOR_NAMES.set("linen", "#faf0e6"); COLOR_NAMES.set("magenta", "#ff00ff"); COLOR_NAMES.set("maroon", "#800000"); COLOR_NAMES.set("mediumaquamarine", "#66cdaa"); COLOR_NAMES.set("mediumblue", "#0000cd"); COLOR_NAMES.set("mediumorchid", "#ba55d3"); COLOR_NAMES.set("mediumpurple", "#9370d8"); COLOR_NAMES.set("mediumseagreen", "#3cb371"); COLOR_NAMES.set("mediumslateblue", "#7b68ee"); COLOR_NAMES.set("mediumspringgreen", "#00fa9a"); COLOR_NAMES.set("mediumturquoise", "#48d1cc"); COLOR_NAMES.set("mediumvioletred", "#c71585"); COLOR_NAMES.set("midnightblue", "#191970"); COLOR_NAMES.set("mintcream", "#f5fffa"); COLOR_NAMES.set("mistyrose", "#ffe4e1"); COLOR_NAMES.set("moccasin", "#ffe4b5"); COLOR_NAMES.set("navajowhite", "#ffdead"); COLOR_NAMES.set("navy", "#000080"); COLOR_NAMES.set("oldlace", "#fdf5e6"); COLOR_NAMES.set("olive", "#808000"); COLOR_NAMES.set("olivedrab", "#6b8e23"); COLOR_NAMES.set("orange", "#ffa500"); COLOR_NAMES.set("orangered", "#ff4500"); COLOR_NAMES.set("orchid", "#da70d6"); COLOR_NAMES.set("palegoldenrod", "#eee8aa"); COLOR_NAMES.set("palegreen", "#98fb98"); COLOR_NAMES.set("paleturquoise", "#afeeee"); COLOR_NAMES.set("palevioletred", "#d87093"); COLOR_NAMES.set("papayawhip", "#ffefd5"); COLOR_NAMES.set("peachpuff", "#ffdab9"); COLOR_NAMES.set("peru", "#cd853f"); COLOR_NAMES.set("pink", "#ffc0cb"); COLOR_NAMES.set("plum", "#dda0dd"); COLOR_NAMES.set("powderblue", "#b0e0e6"); COLOR_NAMES.set("purple", "#800080"); COLOR_NAMES.set("rebeccapurple", "#663399"); COLOR_NAMES.set("red", "#ff0000"); COLOR_NAMES.set("rosybrown", "#bc8f8f"); COLOR_NAMES.set("royalblue", "#4169e1"); COLOR_NAMES.set("saddlebrown", "#8b4513"); COLOR_NAMES.set("salmon", "#fa8072"); COLOR_NAMES.set("sandybrown", "#f4a460"); COLOR_NAMES.set("seagreen", "#2e8b57"); COLOR_NAMES.set("seashell", "#fff5ee"); COLOR_NAMES.set("sienna", "#a0522d"); COLOR_NAMES.set("silver", "#c0c0c0"); COLOR_NAMES.set("skyblue", "#87ceeb"); COLOR_NAMES.set("slateblue", "#6a5acd"); COLOR_NAMES.set("slategray", "#708090"); COLOR_NAMES.set("snow", "#fffafa"); COLOR_NAMES.set("springgreen", "#00ff7f"); COLOR_NAMES.set("steelblue", "#4682b4"); COLOR_NAMES.set("tan", "#d2b48c"); COLOR_NAMES.set("teal", "#008080"); COLOR_NAMES.set("thistle", "#d8bfd8"); COLOR_NAMES.set("tomato", "#ff6347"); COLOR_NAMES.set("turquoise", "#40e0d0"); COLOR_NAMES.set("violet", "#ee82ee"); COLOR_NAMES.set("wheat", "#f5deb3"); COLOR_NAMES.set("white", "#ffffff"); COLOR_NAMES.set("whitesmoke", "#f5f5f5"); COLOR_NAMES.set("yellow", "#ffff00"); COLOR_NAMES.set("yellowgreen", "#9acd32"); export const DEFAULT_MD_EMBED_CSS = `.snw-reference{display: none;}.excalidraw-md-host{padding:0px 10px}.excalidraw-md-footer{height:5px}foreignObject{background-color:transparent}p{display:block;margin-block-start:1em;margin-block-end:1em;margin-inline-start:0px;margin-inline-end:0px;color:inherit}table,tr,th,td{color:inherit;border:1px solid;border-collapse:collapse;padding:3px}th{font-weight:bold;border-bottom:double;background-color:silver}.copy-code-button{display:none}code[class*=language-],pre[class*=language-]{color:#393a34;font-family:"Consolas","Bitstream Vera Sans Mono","Courier New",Courier,monospace;direction:ltr;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;font-size:.9em;line-height:1.2em;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none}pre>code[class*=language-]{font-size:1em}pre[class*=language-]::-moz-selection,pre[class*=language-] ::-moz-selection,code[class*=language-]::-moz-selection,code[class*=language-] ::-moz-selection{background:#C1DEF1}pre[class*=language-]::selection,pre[class*=language-] ::selection,code[class*=language-]::selection,code[class*=language-] ::selection{background:#C1DEF1}pre[class*=language-]{padding:1em;margin:.5em 0;overflow:auto;background-color:#0000001a}:not(pre)>code[class*=language-]{padding:.2em;padding-top:1px;padding-bottom:1px;background:#f8f8f8;border:1px solid #dddddd}.token.comment,.token.prolog,.token.doctype,.token.cdata{color:green;font-style:italic}.token.namespace{opacity:.7}.token.string{color:#a31515}.token.punctuation,.token.operator{color:#393a34}.token.url,.token.symbol,.token.number,.token.boolean,.token.variable,.token.constant,.token.inserted{color:#36acaa}.token.atrule,.token.keyword,.token.attr-value,.language-autohotkey .token.selector,.language-json .token.boolean,.language-json .token.number,code[class*=language-css]{color:#00f}.token.function{color:#393a34}.token.deleted,.language-autohotkey .token.tag{color:#9a050f}.token.selector,.language-autohotkey .token.keyword{color:#00009f}.token.important{color:#e90}.token.important,.token.bold{font-weight:bold}.token.italic{font-style:italic}.token.class-name,.language-json .token.property{color:#2b91af}.token.tag,.token.selector{color:maroon}.token.attr-name,.token.property,.token.regex,.token.entity{color:red}.token.directive.tag .tag{background:#ffff00;color:#393a34}.line-numbers.line-numbers .line-numbers-rows{border-right-color:#a5a5a5}.line-numbers .line-numbers-rows>span:before{color:#2b91af}.line-highlight.line-highlight{background:rgba(193,222,241,.2);background:-webkit-linear-gradient(left,rgba(193,222,241,.2) 70%,rgba(221,222,241,0));background:linear-gradient(to right,rgba(193,222,241,.2) 70%,rgba(221,222,241,0))}blockquote{ font-style:italic;background-color:rgb(46,43,42,0.1);margin:0;margin-left:1em;border-radius:0 4px 4px 0;border:1px solid hsl(0,80%,32%);border-left-width:8px;border-top-width:0px;border-right-width:0px;border-bottom-width:0px;padding:10px 20px;margin-inline-start:30px;margin-inline-end:30px;}`; export const SCRIPTENGINE_ICON = ``; export const DISK_ICON_NAME = "save"; export const EXPORT_IMG_ICON = ` `; export const EXPORT_IMG_ICON_NAME = `export-img`; export const EXCALIDRAW_ICON = ``;