From b569dac9cecf87d3ea2076e2513d751ff2d10f37 Mon Sep 17 00:00:00 2001 From: Kohei Watanabe Date: Wed, 7 Sep 2022 17:48:18 +0900 Subject: [PATCH] set default title --- app/tsconfig.json | 2 +- app/views/components/editor.tsx | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/app/tsconfig.json b/app/tsconfig.json index 1241306..1071743 100644 --- a/app/tsconfig.json +++ b/app/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "@tsconfig/node18-strictest-esm/tsconfig.json", "compilerOptions": { - "lib": ["dom", "dom.iterable"], + "lib": ["esnext", "dom", "dom.iterable"], "jsx": "react-jsx", "jsxImportSource": "solid-js/h", "noPropertyAccessFromIndexSignature": false diff --git a/app/views/components/editor.tsx b/app/views/components/editor.tsx index f7ab74b..a0c8ddc 100644 --- a/app/views/components/editor.tsx +++ b/app/views/components/editor.tsx @@ -43,14 +43,23 @@ export default (props: { editor.registerUpdateListener(() => editor.update(() => { const root = $getRoot(); - const lines = root.getChildren().map((line, i) => { - if (i === 0) return line.getTextContent().trim(); + const defaultTitle = new Date() + .toLocaleDateString(navigator.language, { + year: "numeric", + month: "2-digit", + day: "2-digit", + }) + .replaceAll("/", "-"); + const [titleNode, ...lineNodes] = root.getChildren(); + const title = + titleNode?.getTextContent().trim() || + (lineNodes.length === 0 ? "" : defaultTitle); + const lines = lineNodes.map((line) => { const indent = $isElementNode(line) ? line.getIndent() : 0; return `${" ".repeat(indent)}${line.getTextContent()}`; }); - const title = lines[0]; - const text = lines.join("\n"); - props.onUpdatePage({ id: props.id, title: title ?? "", text }); + const text = [title, ...lines].join("\n"); + props.onUpdatePage({ id: props.id, title, text }); }) ) );