Skip to content

Commit

Permalink
feat(editor): multiple date/time formats; resolves #291
Browse files Browse the repository at this point in the history
menubar split into multiple components in a new folder
  • Loading branch information
Muhammed-Rahif committed Dec 25, 2024
1 parent eadda9c commit 0fe6f6a
Show file tree
Hide file tree
Showing 14 changed files with 376 additions and 286 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"@tauri-apps/plugin-process": "^2.0.0",
"@tauri-apps/plugin-shell": "^2.0.1",
"caret-pos": "^2.0.0",
"dayjs": "^1.11.13",
"localforage": "^1.10.0",
"lodash.debounce": "^4.0.8",
"lodash.merge": "^4.6.2",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import MenuBar from '@/components/MenuBar.svelte';
import MenuBar from '@/src/lib/components/menubar/MenuBar.svelte';
import EditorTabs from '@/components/EditorTabs.svelte';
import FontDialog from '@/components/font-dialog/FontDialog.svelte';
import Shortcuts from '@/components/Shortcuts.svelte';
Expand Down
116 changes: 116 additions & 0 deletions src/lib/components/EditMenuItems.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<script lang="ts">
import * as Menubar from '@/components/ui/menubar';
import { Notpad } from '@/helpers/notpad';
import * as ContextMenu from '@/components/ui/context-menu';
import dayjs from 'dayjs';
import { onMount } from 'svelte';
type Props = {
type: 'menubar' | 'contextmenu';
};
const { type }: Props = $props();
const dateTimeFormats = ['hh:mm A', 'HH:mm', 'MM/DD/YYYY', 'DD/MM/YYYY', 'YYYY-MM-DD'];
let now: number = $state(Date.now());
onMount(() => {
const ONE_MINUTE = 60 * 1000;
/**
* https://stackoverflow.com/a/10797177/14781260
*/
function repeatEvery(func: () => void, interval: number) {
var now = new Date(),
delay = interval - (now.getTime() % interval);
function start() {
func();
setInterval(func, interval);
}
setTimeout(start, delay);
}
repeatEvery(() => (now = Date.now()), ONE_MINUTE);
});
const menuItems = [
{ label: 'Undo', shortcut: 'Ctrl+Z', action: () => Notpad.editOptions.undo() },
{ label: 'Redo', shortcut: 'Ctrl+Y', action: () => Notpad.editOptions.redo() },
{ type: 'separator' },
{ label: 'Cut', shortcut: 'Ctrl+X', action: () => Notpad.editOptions.cut() },
{ label: 'Copy', shortcut: 'Ctrl+C', action: () => Notpad.editOptions.copy() },
{ label: 'Paste', shortcut: 'Ctrl+V', action: () => Notpad.editOptions.paste() },
{ label: 'Delete', shortcut: 'Delete', action: () => Notpad.editOptions.delete() },
{ type: 'separator' },
{ label: 'Select All', shortcut: 'Ctrl+A', action: () => Notpad.editOptions.selectAll() },
{ label: 'Time/Date' }
// {
// subs: [
// {
// label: 'Bold',
// action: makeBold
// },
// {
// label: 'Italic',
// action: makeItalic
// },
// {
// label: 'Underline',
// action: makeUnderline
// }
// ],
// label: 'Text'
// }
];
const Separator = $derived(type === 'menubar' ? Menubar.Separator : ContextMenu.Separator);
const Sub = $derived(type === 'menubar' ? Menubar.Sub : ContextMenu.Sub);
const SubTrigger = $derived(type === 'menubar' ? Menubar.SubTrigger : ContextMenu.SubTrigger);
const SubContent = $derived(type === 'menubar' ? Menubar.SubContent : ContextMenu.SubContent);
const Item = $derived(type === 'menubar' ? Menubar.Item : ContextMenu.Item);
</script>

{#each menuItems as item}
{#if item.type === 'separator'}
<Separator />
{:else if item.label == 'Time/Date'}
<Sub>
<SubTrigger>{item.label}</SubTrigger>
<SubContent>
{#each dateTimeFormats as format}
{@const dateTimeContent = dayjs(now).format(format)}

<Item
onclick={() => {
Notpad.editOptions.insertOrReplace({
content: dateTimeContent
});
}}
>
{dateTimeContent}
</Item>
{/each}
</SubContent>
</Sub>
<!-- {:else if item.subs}
<Sub>
<SubTrigger>{item.label}</SubTrigger>
<SubContent>
{#each item.subs as subItem}
<Item onclick={subItem.action}>
{subItem.label}
</Item>
{/each}
</SubContent>
</Sub> -->
{:else}
<Menubar.Item onclick={item.action}>
{item.label}
{#if item.shortcut}
<Menubar.Shortcut>{item.shortcut}</Menubar.Shortcut>
{/if}
</Menubar.Item>
{/if}
{/each}
32 changes: 2 additions & 30 deletions src/lib/components/EditorTabs.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import * as ContextMenu from '@/components/ui/context-menu';
import EditorTitle from '@/components/EditorTitle.svelte';
import { activeTabId, editors } from '@/store/store';
import { Notpad } from '@/helpers/notpad';
import { slide } from 'svelte/transition';
import CalendarClockIcon from '@/components/icons/CalendarClock.svelte';
import EditMenuItems from '@/components/EditMenuItems.svelte';
let innerWidth = $state(window.innerWidth);
Expand Down Expand Up @@ -56,34 +55,7 @@
</ContextMenu.Trigger>

<ContextMenu.Content class="w-48">
<ContextMenu.Item onclick={() => Notpad.editOptions.undo()}>
Undo<ContextMenu.Shortcut>Ctrl+Z</ContextMenu.Shortcut>
</ContextMenu.Item>
<ContextMenu.Item onclick={() => Notpad.editOptions.redo()}>
Redo<ContextMenu.Shortcut>Ctrl+Y</ContextMenu.Shortcut>
</ContextMenu.Item>
<ContextMenu.Separator />
<ContextMenu.Item onclick={() => Notpad.editOptions.cut()}>
Cut<ContextMenu.Shortcut>Ctrl+X</ContextMenu.Shortcut>
</ContextMenu.Item>
<ContextMenu.Item onclick={() => Notpad.editOptions.copy()}>
Copy<ContextMenu.Shortcut>Ctrl+C</ContextMenu.Shortcut>
</ContextMenu.Item>
<ContextMenu.Item onclick={() => Notpad.editOptions.paste()}>
Paste<ContextMenu.Shortcut>Ctrl+V</ContextMenu.Shortcut>
</ContextMenu.Item>
<ContextMenu.Item onclick={() => Notpad.editOptions.delete()}>
Delete<ContextMenu.Shortcut>Delete</ContextMenu.Shortcut>
</ContextMenu.Item>
<ContextMenu.Separator />
<ContextMenu.Item onclick={() => Notpad.editOptions.selectAll()}>
Select All
<ContextMenu.Shortcut>Ctrl+A</ContextMenu.Shortcut>
</ContextMenu.Item>
<ContextMenu.Item onclick={() => Notpad.editOptions.insertDateAndTime()}>
Time/Date
<CalendarClockIcon class="ml-auto text-muted-foreground" />
</ContextMenu.Item>
<EditMenuItems type="contextmenu" />
</ContextMenu.Content>
</ContextMenu.Root>
</Tabs.Content>
Expand Down
Loading

0 comments on commit 0fe6f6a

Please sign in to comment.