Skip to content

Commit

Permalink
feat(editor): custom caret options (enable/disable, caret style and c…
Browse files Browse the repository at this point in the history
…aret animation)
  • Loading branch information
Muhammed-Rahif committed Dec 28, 2024
1 parent 802e953 commit 7a5f61e
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 54 deletions.
4 changes: 0 additions & 4 deletions src/lib/components/editor/Editor.css
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,3 @@
.editor-container[data-wrap-lines='false'] .ql-editor > * {
@apply w-max;
}

.fake-caret {
transition: all 250ms cubic-bezier(0, 0.55, 0.45, 1);
}
16 changes: 9 additions & 7 deletions src/lib/components/editor/Editor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,17 @@
});
</script>

<div class="relative h-full overflow-hidden">
<div
class="relative h-full overflow-hidden"
style:--editor-zoom={$settings.zoom}
style:--editor-font-size="{$settings.fontSize}px"
style:--editor-font-family={$settings.fontFamily}
style:--line-no-digits-count={lineNo.toString().length}
>
<div
class="editor-container relative flex overflow-hidden
rounded-none bg-transparent text-sm caret-transparent
duration-300"
style="--editor-font-family: '{$settings.fontFamily}';
--editor-font-size: {$settings.fontSize}px;
--editor-zoom: {$settings.zoom};
--line-no-digits-count: {lineNo.toString().length}"
rounded-none bg-transparent text-sm duration-300"
class:caret-transparent={$settings.caret.enable}
data-line-numbers={$settings.lineNumbers}
data-wrap-lines={$settings.wrapLines}
bind:this={editorContainer}
Expand Down
38 changes: 28 additions & 10 deletions src/lib/components/editor/FakeCaret.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import { position as getCaretPosition } from 'caret-pos';
import { settings } from '@/store/store';
import type Quill from 'quill';
import { cn } from '@/utils';
import { CaretAnimation, CaretStyle } from '@/types/SettingsType';
interface Props {
quill: Quill;
Expand Down Expand Up @@ -46,13 +48,29 @@
});
</script>

<span
class="fake-caret absolute z-0 w-0.5 animate-caret-blink
rounded-[.06em] bg-primary"
bind:this={fakeCaretElement}
style="top: {caret.top}px;
left: {caret.left}px;
height: {caret.height}px;
width: {$settings.zoom * 2}px"
spellcheck="false"
></span>
{#if $settings.caret.enable}
<span
class={cn('fake-caret absolute z-0 w-0.5 animate-caret-blink rounded-[.06em]', {
'[transition:left_600ms,top_600ms]': $settings.caret.animation == CaretAnimation.Slow,
'[transition:left_100ms,top_100ms]': $settings.caret.animation == CaretAnimation.Fast,
'[transition:left_300ms,top_300ms]': $settings.caret.animation == CaretAnimation.Medium,
'[transition:left_0s,top_0s]': $settings.caret.animation == CaretAnimation.Off,
'bg-primary': $settings.caret.style != CaretStyle.HollowBlock,
'border border-primary': $settings.caret.style == CaretStyle.HollowBlock
})}
style:width="calc({$settings.caret.style == CaretStyle.VerticalBar ? '2px' : '1ch'} * var(--editor-zoom))"
style:height={$settings.caret.style == CaretStyle.Underline ? '2px' : `${caret.height}px`}
style:margin-top={$settings.caret.style == CaretStyle.Underline
? `${caret.height + 2}px`
: undefined}
style:top="{caret.top}px"
style:left="{caret.left}px"
bind:this={fakeCaretElement}
></span>
{/if}

<style>
.fake-caret {
transition-timing-function: cubic-bezier(0, 0.55, 0.45, 1);
}
</style>
72 changes: 41 additions & 31 deletions src/lib/components/menubar/SettingsMenu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
<ALargeSmallIcon class="ml-auto text-muted-foreground" />
</Menubar.Item>

<Menubar.Separator />
<Menubar.CheckboxItem
checked={$settings.caret.enable}
onclick={Notpad.settings.toggleCustomCaret}
>
Custom Caret
</Menubar.CheckboxItem>

<Menubar.Separator />
<Menubar.Sub>
<Menubar.SubTrigger>Theme</Menubar.SubTrigger>
Expand Down Expand Up @@ -65,38 +73,40 @@
</Menubar.SubContent>
</Menubar.Sub>

<Menubar.Sub>
<Menubar.SubTrigger>Caret</Menubar.SubTrigger>
<Menubar.SubContent>
<Menubar.Sub>
<Menubar.SubTrigger class="gap-2">Style</Menubar.SubTrigger>
<Menubar.SubContent>
{#each Object.values(CaretStyle) as caretStyle}
<Menubar.CheckboxItem
checked={$settings.caret.style == caretStyle}
onclick={() => Notpad.settings.updateCaret({ style: caretStyle })}
>
{caretStyle}
</Menubar.CheckboxItem>
{/each}
</Menubar.SubContent>
</Menubar.Sub>
{#if $settings.caret.enable}
<Menubar.Sub>
<Menubar.SubTrigger>Caret</Menubar.SubTrigger>
<Menubar.SubContent>
<Menubar.Sub>
<Menubar.SubTrigger class="gap-2">Style</Menubar.SubTrigger>
<Menubar.SubContent>
{#each Object.values(CaretStyle) as caretStyle}
<Menubar.CheckboxItem
checked={$settings.caret.style == caretStyle}
onclick={() => Notpad.settings.updateCaret({ style: caretStyle })}
>
{caretStyle}
</Menubar.CheckboxItem>
{/each}
</Menubar.SubContent>
</Menubar.Sub>

<Menubar.Sub>
<Menubar.SubTrigger class="gap-2">Animation</Menubar.SubTrigger>
<Menubar.SubContent>
{#each Object.values(CaretAnimation) as caretAnimation}
<Menubar.CheckboxItem
checked={$settings.caret.animation == caretAnimation}
onclick={() => Notpad.settings.updateCaret({ animation: caretAnimation })}
>
{caretAnimation}
</Menubar.CheckboxItem>
{/each}
</Menubar.SubContent>
</Menubar.Sub>
</Menubar.SubContent>
</Menubar.Sub>
<Menubar.Sub>
<Menubar.SubTrigger class="gap-2">Animation</Menubar.SubTrigger>
<Menubar.SubContent>
{#each Object.values(CaretAnimation) as caretAnimation}
<Menubar.CheckboxItem
checked={$settings.caret.animation == caretAnimation}
onclick={() => Notpad.settings.updateCaret({ animation: caretAnimation })}
>
{caretAnimation}
</Menubar.CheckboxItem>
{/each}
</Menubar.SubContent>
</Menubar.Sub>
</Menubar.SubContent>
</Menubar.Sub>
{/if}

<Menubar.Separator />
<Menubar.Item
Expand Down
10 changes: 9 additions & 1 deletion src/lib/helpers/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export class Settings {
wrapLines: true,
caret: {
animation: CaretAnimation.Medium,
style: CaretStyle.VerticalBar
style: CaretStyle.VerticalBar,
enable: true
}
};

Expand All @@ -35,4 +36,11 @@ export class Settings {
caret: { ...value.caret, ...caret }
}));
}

toggleCustomCaret() {
settings.update((value) => ({
...value,
caret: { ...value.caret, enable: !value.caret.enable }
}));
}
}
3 changes: 2 additions & 1 deletion src/lib/types/SettingsType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ export interface SettingsType {
*/
wrapLines: boolean;
/**
* Caret's specific configs. Style and Animation.
* Caret's specific configs. Style and Animation. Enable or disable.
*/
caret: {
style: CaretStyle;
animation: CaretAnimation;
enable: boolean;
};
}

0 comments on commit 7a5f61e

Please sign in to comment.