Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix quick notes from notifications #3165

Merged
merged 2 commits into from
Sep 4, 2023
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
1 change: 1 addition & 0 deletions apps/mobile/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const App = () => {
if (appLockMode && appLockMode !== "none") {
useUserStore.getState().lockApp(true);
}
globalThis["IS_MAIN_APP_RUNNING"] = true;
init();
setTimeout(async () => {
SettingsService.onFirstLaunch();
Expand Down
2 changes: 1 addition & 1 deletion apps/mobile/app/screens/editor/tiptap/use-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export const useEditor = (

const reset = useCallback(
async (resetState = true, resetContent = true) => {
currentNote.current?.id && db.fs?.cancel(currentNote.current.id);
currentNote.current?.id && db.fs?.cancel(currentNote.current.id, null);
currentNote.current = null;
loadedImages.current = {};
currentContent.current = null;
Expand Down
55 changes: 52 additions & 3 deletions apps/mobile/app/services/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ import { useRelationStore } from "../stores/use-relation-store";
import { useReminderStore } from "../stores/use-reminder-store";
import { presentDialog } from "../components/dialog/functions";
import NetInfo from "@react-native-community/netinfo";
import { encodeNonAsciiHTML } from "entities";
import { convertNoteToText } from "../utils/note-to-text";


export type Reminder = {
id: string;
type: string;
Expand Down Expand Up @@ -87,6 +89,28 @@ async function getNextMonthlyReminderDate(
return await getNextMonthlyReminderDate(reminder, dayjs().year() + 1);
}

export function textToHTML(src: string) {
return src
.split(/[\r\n]/)
.map((line) =>
line
? `<p data-spacing="single">${encodeLine(line)}</p>`
: `<p data-spacing="single"></p>`
)
.join("");
}

function encodeLine(line: string) {
line = encodeNonAsciiHTML(line);
line = line.replace(/(^ +)|( {2,})/g, (sub, ...args) => {
const [starting, inline] = args;
if (starting) return "&nbsp;".repeat(starting.length);
if (inline) return "&nbsp;".repeat(inline.length);
return sub;
});
return line;
}

async function initDatabase(notes = true) {
if (!db.isInitialized) {
await db.initCollections();
Expand Down Expand Up @@ -207,16 +231,41 @@ const onEvent = async ({ type, detail }: Event) => {
});
if (!db.isInitialized) await db.init();
await db.notes?.init();
await db.notes?.add({

const id = await db.notes?.add({
content: {
type: "tiptap",
data: `<p>${input} </p>`
data: textToHTML(input as string)
}
});

const defaultNotebook = db.settings?.getDefaultNotebook();

if (defaultNotebook) {
if (!defaultNotebook.topic) {
await db.relations?.add(
{ type: "notebook", id: defaultNotebook.id },
{ type: "note", id: id }
);
} else {
await db.notes?.addToNotebook(
{
topic: defaultNotebook.topic,
id: defaultNotebook?.id
},
id
);
}
}

const status = await NetInfo.fetch();
if (status.isInternetReachable) {
try {
await db.sync(false, false);
if (!globalThis["IS_MAIN_APP_RUNNING" as never]) {
await db.sync(false, false);
} else {
console.log("main app running, skipping sync");
}
} catch (e) {
console.log(e, (e as Error).stack);
}
Expand Down
6 changes: 5 additions & 1 deletion apps/mobile/share/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,11 @@ const ShareView = ({ quicknote = false }) => {
});

try {
await db.sync(false, false);
if (!globalThis["IS_MAIN_APP_RUNNING"]) {
await db.sync(false, false);
} else {
console.log("main app running, skipping sync");
}
} catch (e) {
console.log(e, e.stack);
}
Expand Down