Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate commands to command palette (Closes #389) #391

Merged
merged 29 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d0ccc5a
migrate tutorial commands to command pallete
mdroidian Mar 23, 2023
ae8823a
move commands to tutorials.tsx
mdroidian Mar 23, 2023
d20b071
pass extensionAPI to toggleFeature
mdroidian Mar 23, 2023
50c04dd
include removeCommand to newAddCommand return
mdroidian Mar 23, 2023
8aa7028
migrate alert commands to command palette
mdroidian Mar 23, 2023
ba608a4
update extensionAPI param type
mdroidian Mar 24, 2023
3ccadf2
remove newAddCommand, migrate workBench commands
mdroidian Mar 24, 2023
d4d78f7
migrate decorator command to command palette
mdroidian Mar 26, 2023
ccf5232
minor updates
mdroidian Mar 26, 2023
3eb2707
decouple toggleFeature and toggleDecorations
mdroidian Mar 26, 2023
3d3ccae
remove file
mdroidian Mar 26, 2023
e8fb9ec
migrate 3+ modules addCommands to command palette
mdroidian Mar 26, 2023
31a12e3
migrate DNP Popup commands
mdroidian Mar 27, 2023
5dfb015
minor changes
mdroidian Mar 27, 2023
654a756
fix toggleFocus function error
mdroidian Mar 27, 2023
7451e61
migrate 3 more modules to command palette
mdroidian Mar 28, 2023
f1dff63
add getHTMLElementFromUid
mdroidian Mar 29, 2023
ed1661a
use activeElement to handle .blur
mdroidian Mar 29, 2023
b21a052
migrate jumpNav (Hot Keys) to commandPalette
mdroidian Mar 30, 2023
f9bfbe5
add setTimeout to cursor placements + formatting
mdroidian Mar 30, 2023
4629860
update timeout to 300
mdroidian Mar 30, 2023
830cdd8
remove Deep Nav from dropdown
mdroidian Mar 30, 2023
0a42854
0.0.1
mdroidian Mar 30, 2023
dadf190
type update
mdroidian Mar 30, 2023
239152e
Merge branch 'main' of https://github.com/mdroidian/roamjs-workbench …
mdroidian Mar 30, 2023
dac7911
DRY refactor commands
mdroidian Mar 30, 2023
630cc94
move event listener
mdroidian Mar 31, 2023
c1d368c
add previous default hotkeys
mdroidian Apr 1, 2023
73cfcfe
Remove default for DNP Jump Date Forward/Backward
mdroidian Apr 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/data/quickReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ const quickReference: {
c1: "Follow me on Twitter at <a href='https://twitter.com/dvargas92495' target='_blank'>@dvargas92495</a><br/>DM me with bugs, suggestions and whatever<br/><br/>",
},
{
c1: "Follow RoamJS at <a href='https://twitter.com/roam_jd' target='_blank'>@roam_jd</a><br/>for all of the latest news and updates surrounding our extensions.<br/><br/>",
c1: "Follow RoamJS at <a href='https://twitter.com/roam_js' target='_blank'>@roam_jd</a><br/>for all of the latest news and updates surrounding our extensions.<br/><br/>",
},
{
c1: "<b>Credits to:</b><br/> <a href='https://twitter.com/tfthacker' target='_blank'>@TfTHacker</a> for being the original author of this extension<br /><a href='https://www.roamhacks.com/' target='_blank'>RoamHacks.com</a> for their tips <br/><a href='https://twitter.com/beauhaan' target='_blank'>Beau Haan</a> for the creative input, follow him on Twitter <br/><a href='https://twitter.com/billpetro' target='_blank'>Bill Petro</a> for experience and calmness. ",
Expand Down
23 changes: 20 additions & 3 deletions src/features/alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from "@blueprintjs/core";
import parseNlpDate from "roamjs-components/date/parseNlpDate";
import formatDistanceToNow from "date-fns/formatDistanceToNow";
import type { OnloadArgs } from "roamjs-components/types";

type AlertContent = {
when: string;
Expand Down Expand Up @@ -257,11 +258,27 @@ const openAlertDashboard = () =>
renderOverlay({ id: "alert-dashboard", Overlay: AlertDashboard });

let enabled = false;
export const toggleFeature = (flag: boolean) => {
export const toggleFeature = (flag: boolean, extensionAPI: OnloadArgs["extensionAPI"]) => {
enabled = flag;
if (enabled) {
unloads.add(addCommand("Create New Alert", createNewAlert));
unloads.add(addCommand("View Current Alerts", openAlertDashboard));
unloads.add(
addCommand(
{
label: "Create New Alert",
callback: createNewAlert,
},
extensionAPI
Comment on lines +265 to +270
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to change for this PR, but a note for future reference.

In typescript, when there's more than one parameter I often prefer a single object instead of multiple params. So for example, I probably would have done:

addCommand(
        {
          label: "Create New Alert",
          callback: createNewAlert,
          extensionAPI
        },
)

The reason being is life becomes way easier for consumers:

  • Order of parameters no longer matters
  • We could freely make any of the parameters optional
  • Easier to refactor in the future: adding/removing parameters doesn't involve changing all consumers in most cases
  • Parameters are named, giving the consumer extra context on what the function expects

)
);
unloads.add(
addCommand(
{
label: "View Current Alerts",
callback: openAlertDashboard,
},
extensionAPI
)
);
const storage = localStorage.getItem(LOCAL_STORAGE_KEY);
if (storage) {
const { alerts } = JSON.parse(storage) as {
Expand Down
34 changes: 15 additions & 19 deletions src/features/article.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Readability } from "@mozilla/readability";
import TurndownService from "turndown";
import iconv from "iconv-lite";
import charset from "charset";
import { InputTextNode } from "roamjs-components/types/native";
import { InputTextNode, OnloadArgs } from "roamjs-components/types/native";
import updateBlock from "roamjs-components/writes/updateBlock";
import createBlock from "roamjs-components/writes/createBlock";
import getUidsFromId from "roamjs-components/dom/getUidsFromId";
Expand Down Expand Up @@ -314,29 +314,25 @@ const inlineImportArticle = async ({
};

const unloads = new Set<() => void>();
export const toggleFeature = (flag: boolean) => {
export const toggleFeature = (
flag: boolean,
extensionAPI: OnloadArgs["extensionAPI"]
) => {
if (flag) {
unloads.add(
addCommand("Import Article Into Roam", () =>
renderImportArticle(
window.roamAlphaAPI.ui.getFocusedBlock()?.["block-uid"]
)
addCommand(
{
label: "Import Article Into Roam",
callback: () =>
renderImportArticle(
window.roamAlphaAPI.ui.getFocusedBlock()?.["block-uid"]
),
mdroidian marked this conversation as resolved.
Show resolved Hide resolved
defaultHotkey: "alt-shift-i",
},
extensionAPI
)
);

const keydownListener = async (e: KeyboardEvent) => {
if (e.altKey && e.shiftKey && (e.key === "I" || e.code === "KeyI")) {
const target = e.target as HTMLElement;
if (target.tagName === "TEXTAREA") {
const value = (target as HTMLTextAreaElement).value;
const { blockUid } = getUidsFromId(target.id);
await inlineImportArticle({ value, parentUid: blockUid });
}
}
};
document.addEventListener("keydown", keydownListener);
unloads.add(() => document.removeEventListener("keydown", keydownListener));

unloads.add(
registerSmartBlocksCommand({
text: "ARTICLE",
Expand Down
Loading