Skip to content

Commit

Permalink
add preview shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
chibat committed Nov 2, 2023
1 parent 3a6f523 commit c56c942
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
22 changes: 22 additions & 0 deletions islands/PostEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default function Edit(props: { post: Post }) {
}

async function displayPreview() {
text.value = textarea.current.value;
sanitizedHtml.value = await trpc.md2html.query({
source: text.value,
});
Expand All @@ -31,6 +32,12 @@ export default function Edit(props: { post: Post }) {
(hljs as any).highlightAll();
});

useEffect(() => {
if (textarea.current) {
textarea.current.focus();
}
}, textarea.current);

useEffect(() => {
if (!preview.value) {
Mousetrap(textarea.current).bind(
Expand All @@ -40,6 +47,21 @@ export default function Edit(props: { post: Post }) {
save();
},
);
Mousetrap(textarea.current).bind(
"mod+p",
() => {
displayPreview();
return false;
},
);
} else {
Mousetrap.bind(
"mod+p",
() => {
displayEdit();
return false;
},
);
}
}, [preview.value]);

Expand Down
43 changes: 36 additions & 7 deletions islands/PostNew.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,28 @@ export default function Post() {
const sanitizedHtml = useSignal("");
const textarea = createRef();

function displayEdit() {
preview.value = false;
}

async function displayPreview() {
text.value = textarea.current.value;
sanitizedHtml.value = await trpc.md2html.query({
source: text.value,
});
preview.value = true;
}

useEffect(() => {
(hljs as any).highlightAll();
});

useEffect(() => {
if (textarea.current) {
textarea.current.focus();
}
}, textarea.current);

useEffect(() => {
if (!preview.value) {
Mousetrap(textarea.current).bind(
Expand All @@ -25,6 +44,21 @@ export default function Post() {
post();
},
);
Mousetrap(textarea.current).bind(
"mod+p",
() => {
displayPreview();
return false;
},
);
} else {
Mousetrap.bind(
"mod+p",
() => {
displayEdit();
return false;
},
);
}
}, [preview.value]);

Expand All @@ -50,7 +84,7 @@ export default function Post() {
<a
class={!preview.value ? "nav-link active" : "nav-link"}
style={{ cursor: "pointer" }}
onClick={() => preview.value = false}
onClick={displayEdit}
>
Edit
</a>
Expand All @@ -59,12 +93,7 @@ export default function Post() {
<a
class={preview.value ? "nav-link active" : "nav-link"}
style={{ cursor: "pointer" }}
onClick={async () => {
sanitizedHtml.value = await trpc.md2html.query({
source: text.value,
});
preview.value = true;
}}
onClick={displayPreview}
>
Preview
</a>
Expand Down
6 changes: 5 additions & 1 deletion routes/help.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ export default defineRoute(async (req, _ctx) => {
</tr>
<tr>
<td>Send/Save Post</td>
<td>CTRL+Enter</td>
<td>(CTRL|⌘)+Enter</td>
</tr>
<tr>
<td>Switch between Preview and Edit</td>
<td>(CTRL|⌘)+p</td>
</tr>
</tbody>
</table>
Expand Down

0 comments on commit c56c942

Please sign in to comment.