Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions apps/mobile/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,21 @@
"expo-splash-screen",
{
"backgroundColor": "#faf9f5",
"dark": { "backgroundColor": "#121110" },
"dark": {
"backgroundColor": "#121110"
},
"image": "./assets/images/splash-icon.png",
"imageWidth": 76
}
],
"expo-sqlite"
"expo-sqlite",
"expo-image",
[
"expo-image-picker",
{
"photosPermission": "DailyNote adds the photos you pick to the note you are writing. They are copied into the app and stay on this device."
}
]
],
"experiments": {
"typedRoutes": true,
Expand Down
3 changes: 3 additions & 0 deletions apps/mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
"expo": "~57.0.8",
"expo-constants": "~57.0.7",
"expo-crypto": "~57.0.1",
"expo-file-system": "~57.0.1",
"expo-font": "~57.0.1",
"expo-haptics": "~57.0.1",
"expo-image": "~57.0.1",
"expo-image-picker": "~57.0.7",
"expo-linking": "~57.0.4",
"expo-router": "~57.0.9",
"expo-splash-screen": "~57.0.5",
Expand Down
94 changes: 72 additions & 22 deletions apps/mobile/src/app/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { hasCommitmentTag, parseHashtags } from '@dailynote/core';
import {
documentToText,
hasCommitmentTag,
insertImage,
isEmptyDocument,
paragraph,
parseHashtags,
serializeDocument,
setAlign,
toggleBullet,
type Block,
} from '@dailynote/core';
import { Link } from 'expo-router';
import { useCallback, useEffect, useRef, useState } from 'react';
import {
KeyboardAvoidingView,
Platform,
Pressable,
ScrollView,
Text,
TextInput,
View,
} from 'react-native';
import Animated, {
Expand All @@ -18,6 +29,9 @@ import Animated, {
} from 'react-native-reanimated';
import { SafeAreaView } from 'react-native-safe-area-context';

import { BlockEditor, type BlockEditorHandle } from '@/components/block-editor';
import { BlockToolbar } from '@/components/block-toolbar';
import { pickImage } from '@/lib/attachments';
import { todayHeading } from '@/lib/format';
import { haptics } from '@/lib/haptics';
import * as motion from '@/lib/motion';
Expand All @@ -35,13 +49,18 @@ import { useTheme } from '@/theme';
export default function Composer() {
const theme = useTheme();
const { create, promote, notes } = useNotes();
const inputRef = useRef<TextInput>(null);
const [body, setBody] = useState('');
const [blocks, setBlocks] = useState<Block[]>(() => [paragraph()]);
const [focused, setFocused] = useState<number | null>(null);
const editor = useRef<BlockEditorHandle>(null);
const [saving, setSaving] = useState(false);
// Bumped after a save: remounting the editor is what clears it and returns
// the caret to a fresh title, since autoFocus only fires on mount.
const [draft, setDraft] = useState(0);

const body = documentToText(blocks);
const tags = parseHashtags(body);
const willCommit = hasCommitmentTag(body);
const canSave = body.trim().length > 0 && !saving;
const canSave = !isEmptyDocument(blocks) && !saving;

// Save pill: springs between its disabled and enabled state instead of
// snapping, and dips under the finger while pressed.
Expand Down Expand Up @@ -81,20 +100,22 @@ export default function Composer() {
}));

const save = useCallback(async () => {
if (body.trim().length === 0 || saving) return;
if (isEmptyDocument(blocks) || saving) return;
setSaving(true);
try {
const note = await create(body);
// Both representations are written together: the document is the note,
// `body` is the plain text everything else reads off it.
const note = await create(body, { doc: serializeDocument(blocks) });
// Writing #do promotes the note to a commitment on the spot -- no extra
// tap, because the tap is what stops people from doing it.
if (hasCommitmentTag(body)) await promote(note.id);
haptics.tap();
setBody('');
inputRef.current?.focus();
setBlocks([paragraph()]);
setDraft((count) => count + 1);
} finally {
setSaving(false);
}
}, [body, saving, create, promote]);
}, [blocks, body, saving, create, promote]);

return (
<SafeAreaView className="flex-1 bg-paper dark:bg-paper-dark" edges={['top', 'left', 'right']}>
Expand Down Expand Up @@ -138,18 +159,24 @@ export default function Composer() {
{todayHeading()}
</Text>

<TextInput
ref={inputRef}
value={body}
onChangeText={setBody}
autoFocus
multiline
textAlignVertical="top"
placeholder="What's on your mind?"
placeholderTextColor={theme.faint}
className="flex-1 px-5 pt-2 font-serif text-[21px] leading-8 text-ink web:outline-none dark:text-ink-dark"
selectionColor={theme.accent}
/>
<ScrollView
className="flex-1"
contentContainerClassName="grow pt-2"
keyboardDismissMode="on-drag"
// So a tap on the surface below the last line reaches it instead
// of only dismissing the keyboard.
keyboardShouldPersistTaps="handled"
>
<BlockEditor
key={draft}
ref={editor}
blocks={blocks}
onChange={setBlocks}
onFocusChange={setFocused}
placeholder="What's on your mind?"
autoFocus
/>
</ScrollView>

<Animated.View
layout={motion.layout}
Expand Down Expand Up @@ -191,6 +218,29 @@ export default function Composer() {
</Animated.View>
) : null}
</Animated.View>

<BlockToolbar
block={focused === null ? undefined : blocks[focused]}
onToggleBullet={() => {
if (focused === null) return;
setBlocks(toggleBullet(blocks, focused));
// The toolbar took the tap; give the caret straight back so the
// keyboard never drops and the next word goes where it should.
editor.current?.restoreFocus();
}}
onAlign={(align) => {
if (focused === null) return;
setBlocks(setAlign(blocks, focused, align));
editor.current?.restoreFocus();
}}
onAddImage={async () => {
const picked = await pickImage();
if (picked === null) return;
// Functional update: the picker is a round trip through another
// screen, and `blocks` here is from before it opened.
setBlocks((current) => insertImage(current, focused, picked));
}}
/>
</Animated.View>
</KeyboardAvoidingView>
</SafeAreaView>
Expand Down
120 changes: 97 additions & 23 deletions apps/mobile/src/app/note/[id].tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
import { threadOf, type Note } from '@dailynote/core';
import {
insertImage,
parseDocument,
setAlign,
threadOf,
toggleBullet,
type Block,
type Note,
} from '@dailynote/core';
import { Stack, useLocalSearchParams, useRouter } from 'expo-router';
import { useMemo, useState } from 'react';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import {
KeyboardAvoidingView,
Platform,
Pressable,
ScrollView,
Text,
TextInput,
View,
} from 'react-native';
import Animated from 'react-native-reanimated';

import { BlockEditor, type BlockEditorHandle } from '@/components/block-editor';
import { BlockToolbar } from '@/components/block-toolbar';
import { pickImage } from '@/lib/attachments';
import { comesBack, formatDay } from '@/lib/format';
import { haptics } from '@/lib/haptics';
import * as motion from '@/lib/motion';
import { AnimatedPressable } from '@/lib/motion';
import { useNotes } from '@/store/notes-store';
import { useTheme } from '@/theme';

/** Note detail: edit the body, promote to a commitment, read the thread. */
/** How long the writing has to stop before an edit is written to SQLite. */
const AUTOSAVE_MS = 600;

/** Note detail: edit the note, promote to a commitment, read the thread. */
export default function NoteDetail() {
const { id } = useLocalSearchParams<{ id: string }>();
const { notes } = useNotes();
Expand All @@ -36,17 +48,56 @@ export default function NoteDetail() {
);
}

// Keyed on the note id so the draft initialises from the stored body once,
// on mount, rather than being synced back by an effect -- store refreshes
// must never clobber what the user is currently typing.
// Keyed on the note id so the draft initialises from storage once, on mount,
// rather than being synced back by an effect -- store refreshes must never
// clobber what the user is currently typing.
return <NoteEditor key={note.id} note={note} />;
}

function NoteEditor({ note }: { note: Note }) {
const theme = useTheme();
const router = useRouter();
const { notes, setBody, softDelete, promote } = useNotes();
const [draft, setDraft] = useState(note.body);
const { notes, setContent, softDelete, promote } = useNotes();
const [blocks, setBlocks] = useState<Block[]>(() => parseDocument(note.doc, note.body));
const [focused, setFocused] = useState<number | null>(null);
const editor = useRef<BlockEditorHandle>(null);

// Autosave, because there is no longer one input whose blur means "done":
// with a block per line, blur fires every time the caret moves between them.
// So the note is written once the writing pauses, and again on the way out.
const latest = useRef(blocks);
const unsaved = useRef(false);
const timer = useRef<ReturnType<typeof setTimeout> | null>(null);

const flush = useCallback(() => {
if (!unsaved.current) return;
unsaved.current = false;
void setContent(note.id, latest.current);
}, [note.id, setContent]);

// Kept in a ref so the unmount effect below can stay on empty deps. `flush`
// closes over `setContent`, whose identity changes on every store refresh --
// an effect depending on it would re-run its cleanup, and saving from a
// cleanup that fires on every save is a loop.
const flushRef = useRef(flush);
useEffect(() => {
flushRef.current = flush;
});

useEffect(() => {
return () => {
if (timer.current !== null) clearTimeout(timer.current);
flushRef.current();
};
}, []);

const change = useCallback((next: Block[]) => {
setBlocks(next);
latest.current = next;
unsaved.current = true;

if (timer.current !== null) clearTimeout(timer.current);
timer.current = setTimeout(() => flushRef.current(), AUTOSAVE_MS);
}, []);

const updates = useMemo(
() => threadOf(notes, note.rootId).filter((candidate) => candidate.id !== note.id),
Expand Down Expand Up @@ -78,21 +129,21 @@ function NoteEditor({ note }: { note: Note }) {
}}
/>

<ScrollView contentContainerClassName="pb-10" keyboardDismissMode="on-drag">
<Text className="pt-1 text-center text-xs text-faint dark:text-faint-dark">
<ScrollView
contentContainerClassName="pb-10 pt-3"
keyboardDismissMode="on-drag"
keyboardShouldPersistTaps="handled"
>
<Text className="pb-3 text-center text-xs text-faint dark:text-faint-dark">
{formatDay(note.createdAt)}
</Text>

<TextInput
value={draft}
onChangeText={setDraft}
onBlur={() => {
if (draft !== note.body) void setBody(note.id, draft);
}}
multiline
textAlignVertical="top"
className="min-h-[220px] px-5 pt-3 font-serif text-[21px] leading-8 text-ink web:outline-none dark:text-ink-dark"
selectionColor={theme.accent}
<BlockEditor
ref={editor}
blocks={blocks}
onChange={change}
onFocusChange={setFocused}
placeholder="Untitled"
/>

{/* The follow-through loop's entry point. Promoting crossfades the
Expand Down Expand Up @@ -152,6 +203,29 @@ function NoteEditor({ note }: { note: Note }) {
</View>
) : null}
</ScrollView>

<BlockToolbar
block={focused === null ? undefined : blocks[focused]}
onToggleBullet={() => {
if (focused === null) return;
change(toggleBullet(blocks, focused));
// The toolbar took the tap; give the caret straight back so the
// keyboard never drops and the next word goes where it should.
editor.current?.restoreFocus();
}}
onAlign={(align) => {
if (focused === null) return;
change(setAlign(blocks, focused, align));
editor.current?.restoreFocus();
}}
onAddImage={async () => {
const picked = await pickImage();
if (picked === null) return;
// `latest` rather than `blocks`: the picker is a round trip through
// another screen, and this closure is from before it opened.
change(insertImage(latest.current, focused, picked));
}}
/>
</KeyboardAvoidingView>
);
}
Loading
Loading