Skip to content

Commit

Permalink
web: add support for multiline titles
Browse files Browse the repository at this point in the history
Signed-off-by: 01zulfi <[email protected]>
  • Loading branch information
01zulfi committed Jan 8, 2025
1 parent 71db854 commit 8a8f14c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
24 changes: 19 additions & 5 deletions apps/web/src/components/editor/title-box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import React, { useCallback, useEffect, useMemo, useRef } from "react";
import { Input } from "@theme-ui/components";
import { Textarea } from "@theme-ui/components";
import { useEditorStore } from "../../stores/editor-store";
import { debounceWithId } from "@notesnook/common";
import useMobile from "../../hooks/use-mobile";
Expand All @@ -37,7 +37,7 @@ type TitleBoxProps = {

function TitleBox(props: TitleBoxProps) {
const { readonly, id } = props;
const inputRef = useRef<HTMLInputElement>(null);
const inputRef = useRef<HTMLTextAreaElement>(null);
const pendingChanges = useRef(false);
// const id = useStore((store) => store.session.id);
const sessionType = useEditorStore((store) => store.getActiveSession()?.type);
Expand Down Expand Up @@ -113,7 +113,7 @@ function TitleBox(props: TitleBoxProps) {
}, [updateFontSize]);

return (
<Input
<Textarea
ref={inputRef}
variant="clean"
id="editor-title"
Expand All @@ -122,18 +122,32 @@ function TitleBox(props: TitleBoxProps) {
placeholder={strings.noteTitle()}
readOnly={readonly}
dir="auto"
wrap="soft"
rows={1}
sx={{
p: 0,
fontFamily,
fontSize: ["1.625em", "1.625em", "2.625em"],
fontWeight: "heading",
width: "100%",
fieldSizing: "content",
whiteSpace: "pre-wrap",
resize: "none",
overflow: "hidden",
"::placeholder": {
color: "placeholder"
}
}}
onKeyDown={(e) => {
if (e.key === "Enter") {
if (e.shiftKey) return;
e.preventDefault();
}
}}
onKeyUp={(e) => {
if (e.key === "Enter") {
if (e.shiftKey) return;
e.preventDefault();
const context = useEditorManager.getState().getEditor(id);
if (!context) return;
context.editor?.focus({ scrollIntoView: true });
Expand Down Expand Up @@ -176,8 +190,8 @@ function textLengthToFontSize(length: number, max: number) {
}

function withSelectionPersist(
input: HTMLInputElement,
action: (input: HTMLInputElement) => void
input: HTMLTextAreaElement,
action: (input: HTMLTextAreaElement) => void
) {
const selection = {
start: input.selectionStart,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/__tests__/notes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ test("note title should allow trailing space", () =>
}
));

test("note title should not allow newlines", () =>
test("note title should allow newlines", () =>
noteTest({ title: "Hello\nhello", content: TEST_NOTE.content }).then(
async ({ db, id }) => {
const note = await db.notes.note(id);
expect(note?.title).toBe("Hello hello");
expect(note?.title).toBe("Hello\nhello");
}
));

Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/collections/notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

import { getId } from "../utils/id.js";
import { getContentFromData } from "../content-types/index.js";
import { NEWLINE_STRIP_REGEX, formatTitle } from "../utils/title-format.js";
import { formatTitle } from "../utils/title-format.js";
import { clone } from "../utils/clone.js";
import { Tiptap } from "../content-types/tiptap.js";
import { EMPTY_CONTENT } from "./content.js";
Expand Down Expand Up @@ -110,7 +110,6 @@ export class Notes implements ICollection {
}

if (typeof item.title !== "undefined") {
item.title = item.title.replace(NEWLINE_STRIP_REGEX, " ");
dateEdited = Date.now();
}

Expand Down
3 changes: 0 additions & 3 deletions packages/core/src/utils/title-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { TimeFormat } from "../types.js";
import { formatDate } from "./date.js";

export const NEWLINE_STRIP_REGEX = /[\r\n\t\v]+/gm;

const DATE_REGEX = /\$date\$/g;
const COUNT_REGEX = /\$count\$/g;
const TIME_REGEX = /\$time\$/g;
Expand All @@ -48,7 +46,6 @@ export function formatTitle(
const timestamp = `${date}${time}`.replace(DATE_TIME_STRIP_REGEX, "");

return titleFormat
.replace(NEWLINE_STRIP_REGEX, " ")
.replace(DATE_REGEX, date)
.replace(TIME_REGEX, time)
.replace(HEADLINE_REGEX, headline || "")
Expand Down

0 comments on commit 8a8f14c

Please sign in to comment.