diff --git a/src/js/Entities/CID.ts b/src/js/Entities/CID.ts deleted file mode 100644 index cc31c806..00000000 --- a/src/js/Entities/CID.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { CID as CoreCID } from "multiformats/cid"; - -export const CID = (() => { - return { - toString: function (cid: CoreCID): string { - return cid.toV1().toString(); - }, - fromString: function (cid: string): CoreCID { - return CoreCID.parse(cid); - }, - }; -})(); diff --git a/src/js/components/atoms/div/wordParts.ts b/src/js/components/atoms/div/wordParts.ts index f388cb14..668c6e30 100644 --- a/src/js/components/atoms/div/wordParts.ts +++ b/src/js/components/atoms/div/wordParts.ts @@ -3,10 +3,13 @@ export const wordParts = (word: string, match: string) => { let indexInWord = 0; return parts + // @ts-ignore js-to-ts wider refactoring required .reduce((acc, part, i) => { return [...acc, ...(i === 0 ? [part] : [match, part])]; }, []) + // @ts-ignore js-to-ts wider refactoring required .filter((p) => p !== "") + // @ts-ignore js-to-ts wider refactoring required .map((syllable) => { indexInWord = indexInWord + syllable.length; const start = indexInWord - syllable.length; diff --git a/src/js/components/organisms/commander/commander.ts b/src/js/components/organisms/commander/commander.ts index 4c96d69e..89ef9b90 100644 --- a/src/js/components/organisms/commander/commander.ts +++ b/src/js/components/organisms/commander/commander.ts @@ -16,25 +16,24 @@ import { button } from "../../atoms/button/button.js"; import { requestNotificationPermission } from "../../../registerServiceWorker.js"; import storage from "../../../utils/localstorage.js"; -const getShortcut = (key) => { +const getShortcut = (key: string | Array): string => { if (Array.isArray(key)) { return key.map((k) => getShortcut(k)).join(", "); } - return key ? `⌘+${key.toUpperCase()}` : ""; }; const commander = (() => { - const commanderModes = { - off: "off", - notes: "notes", - revisions: "revisions", - commands: "commands", - gists: "gists", - }; + enum CommanderModes { + off = "off", + notes = "notes", + revisions = "revisions", + commands = "commands", + gists = "gists", + } let state = { input: "", - mode: commanderModes.off, + mode: CommanderModes.off, options: { selected: 0, length: 0, @@ -45,38 +44,38 @@ const commander = (() => { return state; }, getModes: function () { - return commanderModes; + return CommanderModes; }, - setState: function (newState) { + setState: function (newState: Record) { state = { ...state, ...newState, }; return state; }, - show: function (what = commanderModes.commands) { + show: function (what = CommanderModes.commands) { select("#commander").show(); select("#commander input").focus(); switch (what) { - case commanderModes.commands: + case CommanderModes.commands: this.generateCommands(); select("#commander input").setValue("> "); this.setState({ - mode: commanderModes.commands, + mode: CommanderModes.commands, }); break; - case commanderModes.notes: + case CommanderModes.notes: this.generateNotes(); select("#commander input").setValue(""); this.setState({ - mode: commanderModes.notes, + mode: CommanderModes.notes, }); break; - case commanderModes.revisions: + case CommanderModes.revisions: this.generateRevisions(); select("#commander input").setValue(""); this.setState({ - mode: commanderModes.revisions, + mode: CommanderModes.revisions, }); break; default: @@ -87,12 +86,12 @@ const commander = (() => { }, hide: function () { select("#commander").hide(); - state.mode = commanderModes.off; + state.mode = CommanderModes.off; return this; }, - toggle: function (mode) { + toggle: function (mode?: CommanderModes) { requestNotificationPermission(); - if (state.mode === commanderModes.off || state.mode !== mode) { + if (state.mode === CommanderModes.off || state.mode !== mode) { this.show(mode); } else { this.hide(); @@ -127,6 +126,7 @@ const commander = (() => { .map((command) => { select(".mobile-dock").append( button( + // @ts-ignore js-to-ts wider refactoring required [command.icon, document.createTextNode(command.sortTitle)], command.call, command.title.toLowerCase().replace(/\s/g, "-"), @@ -142,14 +142,14 @@ const commander = (() => { .listen("keydown", (e: KeyboardEvent) => { // arrow down 40 if (e.keyCode === 40) { - if (state.mode === commanderModes.revisions) { + if (state.mode === CommanderModes.revisions) { select("#commands li.selected").click(); } this.selectOption("down"); } // arrow up 38 if (e.keyCode === 38) { - if (state.mode === commanderModes.revisions) { + if (state.mode === CommanderModes.revisions) { select("#commands li.selected").click(); } this.selectOption("up"); @@ -158,7 +158,7 @@ const commander = (() => { .listen("keyup", (e: KeyboardEvent) => { // enter if (e.keyCode === 13) { - if (state.mode === commanderModes.commands) { + if (state.mode === CommanderModes.commands) { select("#commands li.selected div").click(); } else { select("#commands li.selected a").click(); @@ -168,11 +168,12 @@ const commander = (() => { if (e.keyCode === 27) { select(".terminal").focus(); } - if (state.input !== e.target.value) { + const el = e.target as HTMLTextAreaElement; + if (state.input !== el.value) { state.options.selected = 0; } - state.input = e.target.value; - this.generateOptions(e.target.value); + state.input = el.value; + this.generateOptions(el.value); }); return this; }, @@ -185,52 +186,55 @@ const commander = (() => { }, generateRevisions: function () { this.show(); - state.mode = commanderModes.revisions; - const { revisions } = getNote(); - const indexToSelect = state.options.selected; - const revisionsOptions = Object.keys(revisions) - .sort((a, b) => { - const { dateCreated: aDateCreated } = revisions[a]; - const { dateCreated: bDateCreated } = revisions[b]; - return bDateCreated - aDateCreated; - }) - .map((id, i) => ({ - title: div({ content: `...${id.slice(-10)}` }), - secondary: `${relativeDate(revisions[id].dateCreated)}`, - onclick: () => { - url.set(undefined, { - v: id, - }); - state.options.selected = i; - this.generateRevisions(); - }, - })) - .map((r, i) => command(r, i === indexToSelect)); - - select("#commands").html(revisionsOptions); + state.mode = CommanderModes.revisions; + const note = getNote(); + if (note) { + const { revisions } = note; + const indexToSelect = state.options.selected; + const revisionsOptions = Object.keys(revisions) + .sort((a, b) => { + const { dateCreated: aDateCreated } = revisions[a]; + const { dateCreated: bDateCreated } = revisions[b]; + return bDateCreated - aDateCreated; + }) + .map((id, i) => ({ + title: div({ content: `...${id.slice(-10)}` }), + secondary: `${relativeDate(revisions[id].dateCreated)}`, + onclick: () => { + url.set(undefined, { + v: id, + }); + state.options.selected = i; + this.generateRevisions(); + }, + })) + // @ts-ignore js-to-ts wider refactoring required + .map((r, i) => command(r, i === indexToSelect)); + select("#commands").html(revisionsOptions); + state.options = { + ...state.options, + length: revisionsOptions.length, + }; + } - state.options = { - ...state.options, - length: revisionsOptions.length, - }; return this; }, - generateOptions: function (value) { + generateOptions: function (value: string) { switch (state.mode) { - case commanderModes.commands: - case commanderModes.notes: + case CommanderModes.commands: + case CommanderModes.notes: if (value.slice(0, 1) === ">") { - state.mode = commanderModes.commands; + state.mode = CommanderModes.commands; this.generateCommands(value.slice(1).trim()); select("#commander input").placeholder("Search for commands..."); } else { - state.mode = commanderModes.notes; + state.mode = CommanderModes.notes; this.generateNotes(value); } return; - case commanderModes.revisions: + case CommanderModes.revisions: return this.generateRevisions(); - case commanderModes.off: + case CommanderModes.off: break; default: break; @@ -248,11 +252,10 @@ const commander = (() => { const bDateCreated = getDateCreatedFromTitle(b.title); return bDateCreated - aDateCreated; }) - .map(({ id, title, cid }, i) => { + .map(({ id, title }, i) => { const dateCreated = getDateCreatedFromTitle(title); - const linkParams = cid ? `?cid=${cid}` : ""; const href = - `${globalThis.location.origin}${globalThis.location.pathname}#${id}${linkParams}`; + `${globalThis.location.origin}${globalThis.location.pathname}#${id}`; const noteLink = link( div({ content: title, highlight: value }), @@ -260,6 +263,7 @@ const commander = (() => { ); const noteCommand = command( + // @ts-ignore until command.js makes it to ts { title: noteLink, secondary: relativeDate(dateCreated), @@ -286,11 +290,14 @@ const commander = (() => { const indexToSelect = state.options.selected; const commandComponents = this.commands() .filter(({ title }) => smartFilter(title, value)) + // @ts-ignore js-to-ts wider refactoring required .filter(({ experimental = false }) => { return storage.get("__experimental__") ? true : !experimental; }) + // @ts-ignore js-to-ts wider refactoring required .map(({ title, key, call, icon }, i) => { const commandComponent = command( + // @ts-ignore until command.js makes it to ts { title: div({ content: title, highlight: value }), icon: icon, diff --git a/src/js/components/organisms/commander/commands.ts b/src/js/components/organisms/commander/commands.ts index 5554aeae..9292c5e9 100644 --- a/src/js/components/organisms/commander/commands.ts +++ b/src/js/components/organisms/commander/commands.ts @@ -27,26 +27,26 @@ import { copyToClipboard } from "../../../utils/copyToClipboard.ts"; import { sleep } from "../../../utils/sleep.js"; import { publishGist, updateGist } from "../../../utils/github/api.ts"; import { icon } from "../../atoms/icon/icon.js"; -import ListSVG from "../../../../assets/svg/list.svg"; -import TrashSVG from "../../../../assets/svg/trash.svg"; -import CheckmarkCircleSVG from "../../../../assets/svg/checkmark-circle.svg"; -import CloudSyncSVG from "../../../../assets/svg/cloud-sync.svg"; -import LighterSVG from "../../../../assets/svg/lighter.svg"; -import EnterDownSVG from "../../../../assets/svg/enter-down.svg"; -import DownloadSVG from "../../../../assets/svg/download.svg"; -import EnvelopeSVG from "../../../../assets/svg/envelope.svg"; -import BugSVG from "../../../../assets/svg/bug.svg"; -import PictureSVG from "../../../../assets/svg/picture.svg"; -import PrinterSVG from "../../../../assets/svg/printer.svg"; -import PageBreakSVG from "../../../../assets/svg/page-break.svg"; -import FrameExpandSVG from "../../../../assets/svg/frame-expand.svg"; -import ArrowRightCircleSVG from "../../../../assets/svg/arrow-right-circle.svg"; -import MagicWandSVG from "../../../../assets/svg/magic-wand.svg"; -import RocketSVG from "../../../../assets/svg/rocket.svg"; -import SpellCheckSVG from "../../../../assets/svg/spell-check.svg"; -import PencilSVG from "../../../../assets/svg/pencil.svg"; -import ShareSVG from "../../../../assets/svg/exit-up.svg"; -import LeafSVG from "../../../../assets/svg/leaf.svg"; +import * as ListSVG from "../../../../assets/svg/list.svg"; +import * as TrashSVG from "../../../../assets/svg/trash.svg"; +import * as CheckmarkCircleSVG from "../../../../assets/svg/checkmark-circle.svg"; +import * as CloudSyncSVG from "../../../../assets/svg/cloud-sync.svg"; +import * as LighterSVG from "../../../../assets/svg/lighter.svg"; +import * as EnterDownSVG from "../../../../assets/svg/enter-down.svg"; +import * as DownloadSVG from "../../../../assets/svg/download.svg"; +import * as EnvelopeSVG from "../../../../assets/svg/envelope.svg"; +import * as BugSVG from "../../../../assets/svg/bug.svg"; +import * as PictureSVG from "../../../../assets/svg/picture.svg"; +import * as PrinterSVG from "../../../../assets/svg/printer.svg"; +import * as PageBreakSVG from "../../../../assets/svg/page-break.svg"; +import * as FrameExpandSVG from "../../../../assets/svg/frame-expand.svg"; +import * as ArrowRightCircleSVG from "../../../../assets/svg/arrow-right-circle.svg"; +import * as MagicWandSVG from "../../../../assets/svg/magic-wand.svg"; +import * as RocketSVG from "../../../../assets/svg/rocket.svg"; +import * as SpellCheckSVG from "../../../../assets/svg/spell-check.svg"; +import * as PencilSVG from "../../../../assets/svg/pencil.svg"; +import * as ShareSVG from "../../../../assets/svg/exit-up.svg"; +import * as LeafSVG from "../../../../assets/svg/leaf.svg"; import { share } from "../../../utils/webShare.js"; import { setSavedState } from "../../../ui/functions/savedState.ts"; @@ -78,20 +78,22 @@ const sharePublicLinkCommand = { commander.hide(); await saveNote(select(".terminal").getValue()); const note = getNote(); - const response = await publishGist({ - note, - }); - const rawLink = response.history[0].url; - const gitResponse = await fetch(rawLink).then((response) => - response.json() - ); - const { files } = gitResponse; - const fileContents = Object.values(files); - const [gistFile] = fileContents; - const { raw_url: rawUrl } = gistFile; - const linkToShare = `${url.baseUrl}?raw=${rawUrl}`; - const successMessage = "MiroPad public link copied to clipboard 📋!"; - copyToClipboard(linkToShare, successMessage); + if (note) { + const response = await publishGist({ + note, + }); + const rawLink = response.history[0].url; + const gitResponse = await fetch(rawLink).then((response) => + response.json() + ); + const { files } = gitResponse; + const fileContents = Object.values(files); + const [gistFile] = fileContents; + const { raw_url: rawUrl } = gistFile as { raw_url: string }; + const linkToShare = `${url.baseUrl}?raw=${rawUrl}`; + const successMessage = "MiroPad public link copied to clipboard 📋!"; + copyToClipboard(linkToShare, successMessage); + } }, }; @@ -180,7 +182,7 @@ export const commands = () => { call: async () => { const token = storage.get("authToken"); if (!token) { - return await goAuthenticate(); + return goAuthenticate(); } const gistId = storage.get("gistId"); if (!gistId) { @@ -381,5 +383,5 @@ export const commands = () => { commander.hide(); }, }, - ]; + ] as const; }; diff --git a/src/js/components/organisms/noteManager/noteManager.ts b/src/js/components/organisms/noteManager/noteManager.ts index e15ce742..3abcd214 100644 --- a/src/js/components/organisms/noteManager/noteManager.ts +++ b/src/js/components/organisms/noteManager/noteManager.ts @@ -17,11 +17,14 @@ const encodeTitle = (title: string) => { return encodedTitle; }; -export const getDateCreatedFromTitle = (title: string) => { +export const getDateCreatedFromTitle = (title: string): number => { const titleID = getTitleId(title); const note = getNote(titleID); - const { dateCreated } = note || {}; - return dateCreated; + if (note) { + const { dateCreated } = note; + return dateCreated; + } + throw new Error(`Note "${title}" cannot found!`); }; export const markNoteForDeletion = (id: string) => { @@ -178,7 +181,6 @@ export const updateNote = async (what: string) => { export const saveNote = async ( what = select(".terminal").getValue(), - cid?: string, ) => { await storage.saveToDictionary(what); if (what.length) { @@ -202,18 +204,13 @@ export const saveNote = async ( [hash]: { dateCreated: Date.now(), text: what, - ...(cid ? { cid: cid } : {}), }, }, }), ); url.set(titleID, { v: hash, - ...(cid ? { cid: cid } : {}), }); - if (!cid) { - url.deleteParam("cid"); - } storage.set("lastLocalUpdate", new Date()); notify.success("👌 Note saved!"); setSavedState(); diff --git a/src/js/components/organisms/terminal.ts b/src/js/components/organisms/terminal.ts index 8b85b770..1229d847 100644 --- a/src/js/components/organisms/terminal.ts +++ b/src/js/components/organisms/terminal.ts @@ -1,5 +1,5 @@ import { nanoid } from "nanoid"; -import TrashSVG from "../../../assets/svg/trash.svg"; +import * as TrashSVG from "../../../assets/svg/trash.svg"; import { configuration } from "../../../configuration.ts"; import { autoComplete } from "../../features/autoComplete.ts"; import { setSavedState } from "../../ui/functions/savedState.ts"; diff --git a/src/js/utils/github/api.ts b/src/js/utils/github/api.ts index 100c8888..6294e774 100644 --- a/src/js/utils/github/api.ts +++ b/src/js/utils/github/api.ts @@ -150,7 +150,7 @@ export const publishGist = async ({ token = storage.get("authToken"), }: { note: Note; - token: string; + token?: string; }) => { const response = await fetch("https://api.github.com/gists", { method: "POST",