Skip to content

Commit

Permalink
add default editor state
Browse files Browse the repository at this point in the history
  • Loading branch information
breadchris committed Oct 24, 2023
1 parent afcd967 commit 92cc03a
Show file tree
Hide file tree
Showing 9 changed files with 1,064 additions and 21 deletions.
1,017 changes: 1,017 additions & 0 deletions js/dist/extension/tab.css

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions js/dist/extension/tab.css.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions js/dist/extension/tab.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<link type="text/css" href="./tab.css" />

<title>LunaBrain</title>
</head>
Expand Down
8 changes: 6 additions & 2 deletions js/dist/site/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92096,7 +92096,10 @@ to {
[]
);
const initialValue = (0, import_react63.useMemo)(
() => JSON.parse(localStorage.getItem("content") || "null") || [],
() => JSON.parse(localStorage.getItem("content") || "null") || [{
type: "paragraph",
children: [{ text: "" }]
}],
[]
);
const handleDOMBeforeInput = (0, import_react63.useCallback)(
Expand Down Expand Up @@ -92166,7 +92169,7 @@ to {
if (isAstChange) {
const content3 = JSON.stringify(value);
localStorage.setItem("content", content3);
if (value.length === 1 && value[0].type === "paragraph") {
if (value.length === 1 && value[0].type === "paragraph" && value[0].children.length === 1 && value[0].children[0].type === "text") {
setText(value[0].children[0].text);
}
}
Expand Down Expand Up @@ -92256,6 +92259,7 @@ to {
Transforms.delete(editor);
}
const newProperties = {
// @ts-ignore
type
};
Transforms.setNodes(editor, newProperties, {
Expand Down
4 changes: 2 additions & 2 deletions js/dist/site/index.js.map

Large diffs are not rendered by default.

37 changes: 20 additions & 17 deletions js/src/components/Editor/MarkdownEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { Content } from '@/rpc/content/content_pb';
import { withHistory } from 'slate-history'
import { Editable, ReactEditor, Slate, withReact } from 'slate-react'
import { BulletedListElement } from './custom-types'
import { BulletedListElement, CustomEditor} from './custom-types'
import isHotkey from 'is-hotkey'
import {serialize} from "@/util/slate";
import {Stack, TextField} from "@fluentui/react";
Expand All @@ -23,7 +23,7 @@ import toast from "react-hot-toast";
import {textContent, urlContent} from "@/extension/util";
import {useProjectContext} from "@/providers/ProjectProvider";

const SHORTCUTS = {
const SHORTCUTS: Record<string, string> = {
'*': 'list-item',
'-': 'list-item',
'+': 'list-item',
Expand Down Expand Up @@ -52,21 +52,24 @@ const useOnKeydown = (editor: Editor) => {
return onKeyDown
}

export const MarkdownEditor: React.FC<{initial?: Descendant[]}> = ({
}) => {
const { loadContent } = useProjectContext();
export const MarkdownEditor: React.FC<{ initial?: Descendant[] }> = ({}) => {
const {loadContent} = useProjectContext();

const [text, setText] = useState<string>('');
const [currentInputType, setCurrentInputType] = useState<string>('text');

const renderElement = useCallback(props => <Element {...props} />, [])
const renderElement = useCallback((props: JSX.IntrinsicAttributes & { attributes: any; children: any; element: any; }) =>
<Element {...props} />, [])
const editor = useMemo(
() => withShortcuts(withReact(withHistory(createEditor()))),
[]
)
const initialValue = useMemo(
() =>
JSON.parse(localStorage.getItem('content') || 'null') || [],
JSON.parse(localStorage.getItem('content') || 'null') || [{
type: 'paragraph',
children: [{text: ''}],
}],
[]
)

Expand All @@ -75,12 +78,12 @@ export const MarkdownEditor: React.FC<{initial?: Descendant[]}> = ({
queueMicrotask(() => {
const pendingDiffs = ReactEditor.androidPendingDiffs(editor)

const scheduleFlush = pendingDiffs?.some(({ diff, path }) => {
const scheduleFlush = pendingDiffs?.some(({diff, path}) => {
if (!diff.text.endsWith(' ')) {
return false
}

const { text } = SlateNode.leaf(editor, path)
const {text} = SlateNode.leaf(editor, path)
const beforeText = text.slice(0, diff.start) + diff.text.slice(0, -1)
if (!(beforeText in SHORTCUTS)) {
return
Expand Down Expand Up @@ -108,7 +111,7 @@ export const MarkdownEditor: React.FC<{initial?: Descendant[]}> = ({

const onChange: ToolbarProps["onCheckedValueChange"] = (
e,
{ name, checkedItems }
{name, checkedItems}
) => {
setCurrentInputType(checkedItems[0]);
};
Expand Down Expand Up @@ -151,7 +154,7 @@ export const MarkdownEditor: React.FC<{initial?: Descendant[]}> = ({
const content = JSON.stringify(value)
localStorage.setItem('content', content)
//@ts-ignore
if (value.length === 1 && value[0].type === 'paragraph') {
if (value.length === 1 && value[0].type === 'paragraph' && value[0].children.length === 1 && value[0].children[0].type === 'text') {
//@ts-ignore
setText(value[0].children[0].text)
}
Expand Down Expand Up @@ -184,19 +187,19 @@ export const MarkdownEditor: React.FC<{initial?: Descendant[]}> = ({
aria-label="Text"
name="textOptions"
value="text"
icon={<TextT24Regular />}
icon={<TextT24Regular/>}
/>
<ToolbarRadioButton
aria-label="Link"
name="textOptions"
value="link"
icon={<Link24Regular />}
icon={<Link24Regular/>}
/>
<ToolbarRadioButton
aria-label="prompt"
name="textOptions"
value="prompt"
icon={<BrainCircuit24Regular />}
icon={<BrainCircuit24Regular/>}
/>
</ToolbarRadioGroup>
</Toolbar>
Expand All @@ -219,7 +222,7 @@ export const MarkdownEditor: React.FC<{initial?: Descendant[]}> = ({
)
}

const withShortcuts = editor => {
const withShortcuts = (editor: CustomEditor) => {
const { deleteBackward, insertText } = editor

editor.insertText = text => {
Expand All @@ -244,6 +247,7 @@ const withShortcuts = editor => {
}

const newProperties: Partial<SlateElement> = {
// @ts-ignore
type,
}
Transforms.setNodes<SlateElement>(editor, newProperties, {
Expand Down Expand Up @@ -305,15 +309,14 @@ const withShortcuts = editor => {
return
}
}

deleteBackward(...args)
}
}

return editor
}

const Element = ({ attributes, children, element }) => {
const Element: React.FC<{attributes: any, children: any, element: any}> = ({ attributes, children, element }) => {
switch (element.type) {
case 'block-quote':
return <blockquote {...attributes}>{children}</blockquote>
Expand Down
1 change: 1 addition & 0 deletions js/src/extension/tab.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<link type="text/css" href="./tab.css" />

<title>LunaBrain</title>
</head>
Expand Down
Binary file not shown.
10 changes: 10 additions & 0 deletions pkg/server/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ func (a *APIHTTPServer) NewAPIHandler() http.Handler {

muxRoot := http.NewServeMux()

muxRoot.HandleFunc("/webhook", func(w http.ResponseWriter, r *http.Request) {
// dump http request
dump, err := httputil.DumpRequest(r, true)
if err != nil {
slog.Error("failed to dump request", "error", err)
return
}
slog.Debug("webhook request", "dump", string(dump))
})

// TODO breadchris fix this code, preferably with chi
muxRoot.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
Expand Down

0 comments on commit 92cc03a

Please sign in to comment.