Skip to content

Commit

Permalink
Merge pull request #20 from chibat/shortcut-preview
Browse files Browse the repository at this point in the history
Shortcut preview
  • Loading branch information
chibat authored Nov 2, 2023
2 parents 3a6f523 + 0101de7 commit 9888b32
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
24 changes: 23 additions & 1 deletion 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 @@ -32,14 +33,35 @@ export default function Edit(props: { post: Post }) {
});

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

useEffect(() => {
if (preview.value) {
Mousetrap.bind(
"mod+p",
() => {
displayEdit();
return false;
},
);
} else {
Mousetrap(textarea.current).bind(
"mod+enter",
() => {
text.value = textarea.current.value;
save();
},
);
Mousetrap(textarea.current).bind(
"mod+p",
() => {
displayPreview();
return false;
},
);
}
}, [preview.value]);

Expand Down
45 changes: 37 additions & 8 deletions islands/PostNew.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,52 @@ 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) {
if (preview.value) {
Mousetrap.bind(
"mod+p",
() => {
displayEdit();
return false;
},
);
} else {
Mousetrap(textarea.current).bind(
"mod+enter",
() => {
text.value = textarea.current.value;
post();
},
);
Mousetrap(textarea.current).bind(
"mod+p",
() => {
displayPreview();
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 9888b32

Please sign in to comment.