From 73a9bab5bf00da1e7a0f77142643c553e27bac1e Mon Sep 17 00:00:00 2001 From: Michael Gartner Date: Wed, 3 Jan 2024 18:24:34 -0600 Subject: [PATCH] Add Create WorkBench Table command (#464) * Add Create WorkBench Table command * use workbench addCommand * 1.5.1 --- package-lock.json | 4 ++-- package.json | 2 +- src/features/table.tsx | 39 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index c5ed34e..8a8d689 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "workbench", - "version": "1.5.0", + "version": "1.5.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "workbench", - "version": "1.5.0", + "version": "1.5.1", "dependencies": { "@mozilla/readability": "^0.3.0", "buffer": "^6.0.3", diff --git a/package.json b/package.json index aa296f7..84b3217 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "@types/mozilla-readability": "^0.2.0", "@types/turndown": "^5.0.1" }, - "version": "1.5.0", + "version": "1.5.1", "samepage": { "extends": "node_modules/roamjs-components/package.json" } diff --git a/src/features/table.tsx b/src/features/table.tsx index 14f13da..e126de1 100644 --- a/src/features/table.tsx +++ b/src/features/table.tsx @@ -33,6 +33,9 @@ import getSubTree from "roamjs-components/util/getSubTree"; import getUids from "roamjs-components/dom/getUids"; import setInputSetting from "roamjs-components/util/setInputSetting"; import setInputSettings from "roamjs-components/util/setInputSettings"; +import { OnloadArgs } from "roamjs-components/types"; +import { render as renderToast } from "roamjs-components/components/Toast"; +import { addCommand } from "./workBench"; type ConfigurationProps = { blockUid: string; @@ -577,7 +580,6 @@ const DisplayTable = ({ blockUid, setIsEdit }: DisplayTableProps) => { top: 0, right: 0, bottom: 0, - borderRight: `1px solid rgba(16,22,26,0.15)`, paddingLeft: 5, pointerEvents: "auto", }} @@ -616,7 +618,10 @@ const Table = ({ blockUid }: { blockUid: string }): JSX.Element => { }; const unloads = new Set<() => void>(); -export const toggleFeature = (flag: boolean) => { +export const toggleFeature = ( + flag: boolean, + extensionAPI: OnloadArgs["extensionAPI"] +) => { if (flag) { const tableButtonObserver = createButtonObserver({ attribute: "wb-table", @@ -627,6 +632,35 @@ export const toggleFeature = (flag: boolean) => { )(b); }, }); + + unloads.add( + addCommand( + { + label: "Create Table", + callback: async () => { + const uid = window.roamAlphaAPI.ui.getFocusedBlock()?.["block-uid"]; + if (!uid) { + renderToast({ + id: "workbench-table-create-block", + content: + "Must be focused on a block to create a WorkBench Table", + }); + return; + } + // setTimeout is needed because sometimes block is left blank + setTimeout(async () => { + await updateBlock({ + uid, + text: "{{wb-table}}", + }); + }, 200); + document.querySelector("body")?.click(); + }, + }, + extensionAPI + ) + ); + addStyle(` /* Chrome, Safari, Edge, Opera */ .roamjs-workbench-table-config input::-webkit-outer-spin-button, @@ -658,6 +692,7 @@ export const toggleFeature = (flag: boolean) => { display: none; } `); + unloads.add(() => tableButtonObserver.disconnect()); } else { unloads.forEach((u) => u());