From fcde669685aa402e7668c9212ef4a0b989534b6d Mon Sep 17 00:00:00 2001 From: reya Date: Thu, 4 Jan 2024 10:44:00 +0700 Subject: [PATCH] feat(editor): add hot key and update function --- packages/ark/src/provider.tsx | 11 ++---- packages/ui/package.json | 1 + packages/ui/src/editor/form.tsx | 70 ++++++++++++++++++++++++++++++--- packages/ui/src/editor/utils.ts | 6 +++ packages/ui/src/navigation.tsx | 2 + pnpm-lock.yaml | 13 ++++++ 6 files changed, 90 insertions(+), 13 deletions(-) diff --git a/packages/ark/src/provider.tsx b/packages/ark/src/provider.tsx index 800a4db4..69be7d34 100644 --- a/packages/ark/src/provider.tsx +++ b/packages/ark/src/provider.tsx @@ -108,8 +108,7 @@ const LumeProvider = ({ children }: PropsWithChildren) => { } const explicitRelayUrls = normalizeRelayUrlSet([ - "wss://relay.damus.io", - "wss://relay.nostr.band/all", + "wss://bostr.nokotaro.com/", "wss://nostr.mutinywallet.com", ]); @@ -135,11 +134,9 @@ const LumeProvider = ({ children }: PropsWithChildren) => { explicitRelayUrls, outboxRelayUrls, blacklistRelayUrls, - enableOutboxModel: storage.settings.lowPowerMode - ? false - : storage.settings.outbox, - autoConnectUserRelays: storage.settings.lowPowerMode ? false : true, - autoFetchUserMutelist: storage.settings.lowPowerMode ? false : true, + enableOutboxModel: !storage.settings.lowPowerMode, + autoConnectUserRelays: !storage.settings.lowPowerMode, + autoFetchUserMutelist: !storage.settings.lowPowerMode, // clientName: 'Lume', // clientNip89: '', }); diff --git a/packages/ui/package.json b/packages/ui/package.json index 10059b43..e912f3eb 100644 --- a/packages/ui/package.json +++ b/packages/ui/package.json @@ -24,6 +24,7 @@ "nostr-tools": "~1.17.0", "react": "^18.2.0", "react-dom": "^18.2.0", + "react-hotkeys-hook": "^4.4.1", "react-router-dom": "^6.21.1", "slate": "^0.101.5", "slate-react": "^0.101.5", diff --git a/packages/ui/src/editor/form.tsx b/packages/ui/src/editor/form.tsx index 2fc9e2df..f1008ef6 100644 --- a/packages/ui/src/editor/form.tsx +++ b/packages/ui/src/editor/form.tsx @@ -1,10 +1,18 @@ -import { MentionNote, useStorage } from "@lume/ark"; +import { MentionNote, useArk, useStorage } from "@lume/ark"; import { TrashIcon } from "@lume/icons"; import { NDKCacheUserProfile } from "@lume/types"; import { cn, editorValueAtom } from "@lume/utils"; -import { useAtomValue } from "jotai"; +import { NDKEvent, NDKKind } from "@nostr-dev-kit/ndk"; +import { useAtom } from "jotai"; import { useEffect, useRef, useState } from "react"; -import { Editor, Range, Transforms, createEditor } from "slate"; +import { + Descendant, + Editor, + Node, + Range, + Transforms, + createEditor, +} from "slate"; import { Editable, ReactEditor, @@ -14,6 +22,7 @@ import { useSlateStatic, withReact, } from "slate-react"; +import { toast } from "sonner"; import { User } from "../user"; import { EditorAddMedia } from "./addMedia"; import { @@ -177,10 +186,11 @@ const Element = (props) => { }; export function EditorForm() { + const ark = useArk(); const storage = useStorage(); const ref = useRef(); - const initialValue = useAtomValue(editorValueAtom); + const [editorValue, setEditorValue] = useAtom(editorValueAtom); const [contacts, setContacts] = useState([]); const [target, setTarget] = useState(); const [index, setIndex] = useState(0); @@ -193,6 +203,52 @@ export function EditorForm() { ?.filter((c) => c?.name?.toLowerCase().startsWith(search.toLowerCase())) ?.slice(0, 10); + const reset = () => { + // @ts-expect-error, backlog + editor.children = [{ type: "paragraph", children: [{ text: "" }] }]; + setEditorValue([{ type: "paragraph", children: [{ text: "" }] }]); + }; + + const serialize = (nodes: Descendant[]) => { + return nodes + .map((n) => { + // @ts-expect-error, backlog + if (n.type === "image") return n.url; + // @ts-expect-error, backlog + if (n.type === "event") return n.eventId; + + // @ts-expect-error, backlog + if (n.children.length) { + // @ts-expect-error, backlog + return n.children + .map((n) => { + if (n.type === "mention") return n.npub; + return Node.string(n).trim(); + }) + .join(" "); + } + + return Node.string(n); + }) + .join("\n"); + }; + + const submit = async () => { + const event = new NDKEvent(ark.ndk); + event.kind = NDKKind.Text; + event.content = serialize(editor.children); + + const publish = await event.publish(); + + if (!publish) toast.error("Failed to publish event, try again later."); + + toast.success( + `Event has been published successfully to ${publish.size} relays.`, + ); + + reset(); + }; + useEffect(() => { async function loadContacts() { const res = await storage.getAllCacheUsers(); @@ -216,7 +272,7 @@ export function EditorForm() {
{ const { selection } = editor; @@ -246,6 +302,7 @@ export function EditorForm() { >
)}
-
+
@@ -287,6 +344,7 @@ export function EditorForm() {