Browse Source

Handle in-place editing in preview mode

master
buttercat1791 1 year ago
parent
commit
3d0e860a0b
  1. 39
      src/lib/components/Preview.svelte
  2. 3
      src/routes/new/edit/+page.svelte

39
src/lib/components/Preview.svelte

@ -4,6 +4,8 @@
import { CaretDownSolid, CaretUpSolid, EditOutline } from "flowbite-svelte-icons"; import { CaretDownSolid, CaretUpSolid, EditOutline } from "flowbite-svelte-icons";
import { createEventDispatcher } from "svelte"; import { createEventDispatcher } from "svelte";
// TODO: Push parser to state to be read on reload.
export let sectionClass: string = ''; export let sectionClass: string = '';
export let isSectionStart: boolean = false; export let isSectionStart: boolean = false;
export let rootId: string; export let rootId: string;
@ -13,7 +15,7 @@
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
let isEditing: boolean = false; let isEditing: boolean = false;
let currentContent: string; let currentContent: string = $parser.getContent(rootId);
let hasCursor: boolean = false; let hasCursor: boolean = false;
let childHasCursor: boolean; let childHasCursor: boolean;
@ -56,13 +58,11 @@
childHasCursor = false; childHasCursor = false;
} }
// TODO: Trigger rerender when editing state changes.
const toggleEditing = (id: string, shouldSave: boolean = true) => { const toggleEditing = (id: string, shouldSave: boolean = true) => {
const editing = isEditing; const editing = isEditing;
currentContent = $parser.getContent(id);
if (editing && shouldSave) { if (editing && shouldSave) {
// TODO: Save updated content. $parser.updateEventContent(id, currentContent);
} }
isEditing = !editing; isEditing = !editing;
@ -79,9 +79,36 @@
> >
<!-- Zettel base case --> <!-- Zettel base case -->
{#if orderedChildren.length === 0 || depth >= 4} {#if orderedChildren.length === 0 || depth >= 4}
{#if isEditing}
<form class='w-full'>
<Textarea class='textarea-leather w-full' bind:value={currentContent}>
<div slot='footer' class='flex space-x-2 justify-end'>
<Button
type='reset'
class='btn-leather min-w-fit'
size='sm'
outline
on:click={() => toggleEditing(rootId, false)}
>
Cancel
</Button>
<Button
type='submit'
class='btn-leather min-w-fit'
size='sm'
solid
on:click={() => toggleEditing(rootId, true)}
>
Save
</Button>
</div>
</Textarea>
</form>
{:else}
<P firstupper={isSectionStart}> <P firstupper={isSectionStart}>
{@html $parser.getContent(rootId)} {@html currentContent}
</P> </P>
{/if}
{:else} {:else}
<div class='flex flex-col space-y-2'> <div class='flex flex-col space-y-2'>
<Heading tag={getHeadingTag(depth)} class='h-leather'> <Heading tag={getHeadingTag(depth)} class='h-leather'>
@ -108,7 +135,7 @@
<Button class='btn-leather' size='sm' outline> <Button class='btn-leather' size='sm' outline>
<CaretDownSolid /> <CaretDownSolid />
</Button> </Button>
<Button class='btn-leather' size='sm' outline> <Button class='btn-leather' size='sm' outline on:click={() => toggleEditing(rootId)}>
<EditOutline /> <EditOutline />
</Button> </Button>
<Tooltip class='tooltip-leather' type='auto' placement='top'> <Tooltip class='tooltip-leather' type='auto' placement='top'>

3
src/routes/new/edit/+page.svelte

@ -27,6 +27,7 @@
const prepareReview = () => { const prepareReview = () => {
$parser.reset(); $parser.reset();
$parser.parse($editorText); $parser.parse($editorText);
$parser.generate($ndk.activeUser?.pubkey!);
goto('/new/compose'); goto('/new/compose');
} }
</script> </script>
@ -64,7 +65,7 @@
</ToolbarButton> </ToolbarButton>
</Toolbar> </Toolbar>
{#if rootIndexId} {#if rootIndexId}
<Preview sectionClass='m-2' {rootIndexId} /> <Preview sectionClass='m-2' rootId={rootIndexId} />
{/if} {/if}
</form> </form>
{/if} {/if}

Loading…
Cancel
Save