From a2fe01ca3e722d6d68bd25f3cc2f6ad1ec0b9ab1 Mon Sep 17 00:00:00 2001 From: Loris Sigrist Date: Sat, 14 Oct 2023 13:24:29 +0200 Subject: [PATCH] Editable message --- src/{toolkit => }/ambient.d.ts | 0 src/core/codegen/utils/stringUtils.js | 10 ++++----- src/core/index.js | 4 ---- src/core/runtime/EditableMessage.svelte | 17 ++++++++++++++++ src/core/runtime/T.svelte | 27 ++++++++++++++++++++++--- src/core/utils/logger.js | 8 +++++--- 6 files changed, 51 insertions(+), 15 deletions(-) rename src/{toolkit => }/ambient.d.ts (100%) create mode 100644 src/core/runtime/EditableMessage.svelte diff --git a/src/toolkit/ambient.d.ts b/src/ambient.d.ts similarity index 100% rename from src/toolkit/ambient.d.ts rename to src/ambient.d.ts diff --git a/src/core/codegen/utils/stringUtils.js b/src/core/codegen/utils/stringUtils.js index 915ccad..94489b0 100644 --- a/src/core/codegen/utils/stringUtils.js +++ b/src/core/codegen/utils/stringUtils.js @@ -26,11 +26,11 @@ export const stringTypeUnion = (strings) => { } /** - * Indents some text by one level. + * Indents some text by n spaces. * @param {string} text - */ -export function indent(text) { - const INDENT_SPACES = 4; + * @param {number | undefined} spaces + */ +export function indent(text, spaces = 4) { const lines = text.split("\n"); - return lines.map((l) => " ".repeat(INDENT_SPACES) + l).join("\n"); + return lines.map((l) => " ".repeat(spaces) + l).join("\n"); } diff --git a/src/core/index.js b/src/core/index.js index 6c3c919..0684590 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -202,10 +202,6 @@ export function t18sCore(pluginConfig) { locale, }, }); - } else { - logger.error( - `Could not trigger HMR event '${event}' for locale '${locale}' because the viteDevServer is not available. This should never happen.`, - ); } } diff --git a/src/core/runtime/EditableMessage.svelte b/src/core/runtime/EditableMessage.svelte new file mode 100644 index 0000000..9e6542c --- /dev/null +++ b/src/core/runtime/EditableMessage.svelte @@ -0,0 +1,17 @@ + + + + +{#if editing} + {txt} +{:else} + + editing = true}> + {txt} + +{/if} \ No newline at end of file diff --git a/src/core/runtime/T.svelte b/src/core/runtime/T.svelte index 51f7241..222e326 100644 --- a/src/core/runtime/T.svelte +++ b/src/core/runtime/T.svelte @@ -1,16 +1,37 @@ -{$t(key, values)} +{#if dev} + +{:else} + {txt} +{/if} diff --git a/src/core/utils/logger.js b/src/core/utils/logger.js index 8efb8c0..87799ba 100644 --- a/src/core/utils/logger.js +++ b/src/core/utils/logger.js @@ -1,4 +1,5 @@ import kleur from "kleur"; +import { indent } from "../codegen/utils/stringUtils.js"; /** * Log Messages to the console in a standard format. @@ -66,9 +67,10 @@ export class Logger { * @returns {string} */ #formatMessage(msg, icon = null) { - msg = msg.replace(/^/gm, " "); //Indent each line by 9 spaces (2 spaces + [t18s] + 1 space) - const prefix = icon ? icon + " " + "[t18s] " : " [t18s] "; - msg = prefix + msg.slice(9); //Remove the first 9 spaces + if (msg.trim().length === 0) return msg; //Ignore empty messages + const prefix = `${icon ?? " "} [t18s] `; + msg = indent(msg, prefix.length); + msg = prefix + msg.slice(prefix.length); return msg; } }