diff --git a/apps/client/src/services/clipboard.ts b/apps/client/src/services/clipboard.ts index 002309586..952ac2e22 100644 --- a/apps/client/src/services/clipboard.ts +++ b/apps/client/src/services/clipboard.ts @@ -109,39 +109,6 @@ function isClipboardEmpty() { return clipboardBranchIds.length === 0; } -export function copyText(text: string) { - if (!text) { - return; - } - - let succeeded = false; - - try { - if (navigator.clipboard) { - navigator.clipboard.writeText(text); - succeeded = true; - } else { - // Fallback method: https://stackoverflow.com/a/72239825 - const textArea = document.createElement("textarea"); - textArea.value = text; - document.body.appendChild(textArea); - textArea.focus(); - textArea.select(); - succeeded = document.execCommand('copy'); - document.body.removeChild(textArea); - } - } catch (e) { - console.warn(e); - succeeded = false; - } - - if (succeeded) { - toast.showMessage(t("clipboard.copy_success")); - } else { - toast.showError(t("clipboard.copy_failed")); - } -} - export default { pasteAfter, pasteInto, diff --git a/apps/client/src/services/clipboard_ext.ts b/apps/client/src/services/clipboard_ext.ts new file mode 100644 index 000000000..9ab98af68 --- /dev/null +++ b/apps/client/src/services/clipboard_ext.ts @@ -0,0 +1,37 @@ +export function copyText(text: string) { + if (!text) { + return; + } + try { + if (navigator.clipboard) { + navigator.clipboard.writeText(text); + return true; + } else { + // Fallback method: https://stackoverflow.com/a/72239825 + const textArea = document.createElement("textarea"); + textArea.value = text; + try { + document.body.appendChild(textArea); + textArea.focus(); + textArea.select(); + return document.execCommand('copy'); + } finally { + document.body.removeChild(textArea); + } + } + } catch (e) { + console.warn(e); + return false; + } +} + +export async function copyTextWithToast(text: string) { + const t = (await import("./i18n.js")).t; + const toast = (await import("./toast.js")).default; + + if (copyText(text)) { + toast.showMessage(t("clipboard.copy_success")); + } else { + toast.showError(t("clipboard.copy_failed")); + } +} diff --git a/apps/client/src/services/options.ts b/apps/client/src/services/options.ts index c20c5bfe0..22aaa9cf5 100644 --- a/apps/client/src/services/options.ts +++ b/apps/client/src/services/options.ts @@ -1,4 +1,5 @@ import server from "./server.js"; +import { isShare } from "./utils.js"; type OptionValue = number | string; @@ -7,7 +8,9 @@ class Options { private arr!: Record; constructor() { - this.initializedPromise = server.get>("options").then((data) => this.load(data)); + if (!isShare) { + this.initializedPromise = server.get>("options").then((data) => this.load(data)); + } } load(arr: Record) { diff --git a/apps/client/src/services/server.ts b/apps/client/src/services/server.ts index 8207f18ff..f577fbfb4 100644 --- a/apps/client/src/services/server.ts +++ b/apps/client/src/services/server.ts @@ -1,4 +1,4 @@ -import utils from "./utils.js"; +import utils, { isShare } from "./utils.js"; import ValidationError from "./validation_error.js"; type Headers = Record; @@ -28,6 +28,10 @@ export interface StandardResponse { } async function getHeaders(headers?: Headers) { + if (isShare) { + return {}; + } + const appContext = (await import("../components/app_context.js")).default; const activeNoteContext = appContext.tabManager ? appContext.tabManager.getActiveContext() : null; diff --git a/apps/client/src/services/syntax_highlight.ts b/apps/client/src/services/syntax_highlight.ts index cb2d4d873..6e08af760 100644 --- a/apps/client/src/services/syntax_highlight.ts +++ b/apps/client/src/services/syntax_highlight.ts @@ -2,7 +2,9 @@ import { ensureMimeTypes, highlight, highlightAuto, loadTheme, Themes, type Auto import mime_types from "./mime_types.js"; import options from "./options.js"; import { t } from "./i18n.js"; -import { copyText } from "./clipboard.js"; +import { copyText } from "./clipboard_ext.js"; +import { isShare } from "./utils.js"; +import { MimeType } from "@triliumnext/commons"; let highlightingLoaded = false; @@ -14,9 +16,6 @@ let highlightingLoaded = false; */ export async function formatCodeBlocks($container: JQuery) { const syntaxHighlightingEnabled = isSyntaxHighlightEnabled(); - if (syntaxHighlightingEnabled) { - await ensureMimeTypesForHighlighting(); - } const codeBlocks = $container.find("pre code"); for (const codeBlock of codeBlocks) { @@ -49,11 +48,11 @@ export async function applySingleBlockSyntaxHighlight($codeBlock: JQuery