Skip to content

Commit 61d0e6b

Browse files
authored
add file format to formatConverter, drawer resizer, misc changes (#477)
* add file format to formatConverter, drawer resizer, misc changes * 1.5.4
1 parent 5e2c56c commit 61d0e6b

File tree

3 files changed

+60
-36
lines changed

3 files changed

+60
-36
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"@types/mozilla-readability": "^0.2.0",
2424
"@types/turndown": "^5.0.1"
2525
},
26-
"version": "1.5.3",
26+
"version": "1.5.4",
2727
"samepage": {
2828
"extends": "node_modules/roamjs-components/package.json"
2929
}

src/features/formatConverter.tsx

+57-33
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,28 @@ import getFullTreeByParentUid from "roamjs-components/queries/getFullTreeByParen
2020
import { addCommand } from "./workBench";
2121
import getParentUidByBlockUid from "roamjs-components/queries/getParentUidByBlockUid";
2222

23-
const OPTIONS: Record<string, string> = {
24-
puretext_Space: "Text with space indentation",
25-
puretext_Tab: "Text with tab indentation",
26-
pureText_NoIndentation: "Text with no indentation",
27-
markdown_Github: "GitHub Flavored Markdown",
28-
markdown_Github_flatten: "GitHub Flavored Markdown - flatten",
29-
html_Simple: "HTML",
30-
html_Markdown_Github_flatten: "HTML after Markdown Flattening",
31-
json_Simple: "JSON in simple format",
32-
json_Simple_withIndentation:
33-
"JSON in simple format with Indentation in text string",
23+
const OPTIONS: Record<string, { label: string; fileFormat: string }> = {
24+
puretext_Space: { label: "Text with space indentation", fileFormat: ".txt" },
25+
puretext_Tab: { label: "Text with tab indentation", fileFormat: ".txt" },
26+
pureText_NoIndentation: {
27+
label: "Text with no indentation",
28+
fileFormat: ".txt",
29+
},
30+
markdown_Github: { label: "GitHub Flavored Markdown", fileFormat: ".md" },
31+
markdown_Github_flatten: {
32+
label: "GitHub Flavored Markdown - flatten",
33+
fileFormat: ".md",
34+
},
35+
html_Simple: { label: "HTML", fileFormat: ".html" },
36+
html_Markdown_Github_flatten: {
37+
label: "HTML after Markdown Flattening",
38+
fileFormat: ".html",
39+
},
40+
json_Simple: { label: "JSON in simple format", fileFormat: ".json" },
41+
json_Simple_withIndentation: {
42+
label: "JSON in simple format with Indentation in text string",
43+
fileFormat: ".json",
44+
},
3445
};
3546

3647
const sortObjectsByOrder = (o: TreeNode[]) => {
@@ -397,11 +408,14 @@ const FormatConverterUI = ({
397408
() => localStorage.getItem("formatConverterUI_lastFormat") as string,
398409
[]
399410
);
400-
const [value, setValue] = useState(lastValue || "puretext_Space");
411+
const [selectedFormat, setSelectedFormat] = useState(
412+
lastValue || "puretext_Space"
413+
);
414+
const [drawerSize, setDrawerSize] = useState("50%");
401415
const [displayValue, setDisplayValue] = useState("");
402416
const changeFormat = useCallback(async () => {
403-
localStorage.setItem("formatConverterUI_lastFormat", value);
404-
switch (value) {
417+
localStorage.setItem("formatConverterUI_lastFormat", selectedFormat);
418+
switch (selectedFormat) {
405419
case "puretext_Tab":
406420
setDisplayValue(
407421
await iterateThroughTree(uid, formatter.pureText_TabIndented)
@@ -442,22 +456,24 @@ const FormatConverterUI = ({
442456
setDisplayValue((await flatJson(uid, true)) as string);
443457
break;
444458
}
445-
}, [value, uid]);
459+
}, [selectedFormat, uid]);
446460
useEffect(() => {
447461
changeFormat();
448462
}, [changeFormat]);
449463
return (
450464
<Drawer
465+
title={"WorkBench Format Converter"}
466+
className={"workbench-format-converter pointer-events-auto"}
467+
portalClassName={"pointer-events-none"}
451468
isOpen={isOpen}
452469
onClose={onClose}
453-
title={"WorkBench Format Converter"}
454470
position={"bottom"}
455471
hasBackdrop={false}
456472
canOutsideClickClose={false}
457-
portalClassName={"pointer-events-none"}
458-
className={"pointer-events-auto"}
459473
enforceFocus={false}
460474
autoFocus={false}
475+
size={drawerSize}
476+
canEscapeKeyClose={true}
461477
>
462478
<div
463479
className={Classes.DRAWER_BODY}
@@ -467,18 +483,20 @@ const FormatConverterUI = ({
467483
overflow: "hidden",
468484
}}
469485
>
470-
<div>
471-
<div className="flex items-center justify-between">
472-
<Label style={{ padding: 10, flexGrow: 1 }}>
486+
<div style={{ height: "calc(100% - 100px)" }}>
487+
<div className="flex flex-col md:flex-row items-center mb-2">
488+
{/* Output format select */}
489+
<Label className="p-3" style={{ marginBottom: 0 }}>
473490
Output format:
474491
<MenuItemSelect
475-
activeItem={value}
476-
onItemSelect={(item) => setValue(item)}
492+
activeItem={selectedFormat}
493+
onItemSelect={(item) => setSelectedFormat(item)}
477494
items={Object.keys(OPTIONS)}
478-
transformItem={(k) => OPTIONS[k]}
495+
transformItem={(k) => OPTIONS[k].label}
479496
/>
480497
</Label>
481-
<div className="flex gap-4 items-center">
498+
{/* Middle Buttons */}
499+
<div className="flex p-3 gap-4 items-center justify-center">
482500
<Button
483501
icon={"refresh"}
484502
small
@@ -500,11 +518,12 @@ const FormatConverterUI = ({
500518
title="Save to a file"
501519
onClick={() => {
502520
const currentPageName = getPageTitleByPageUid(uid);
521+
const extension = OPTIONS[selectedFormat].fileFormat;
503522
const filename =
504523
(currentPageName + "-" + new Date().toISOString()).replace(
505524
/(\W+)/gi,
506525
"-"
507-
) + ".txt";
526+
) + extension;
508527
const element = document.createElement("a");
509528
element.setAttribute(
510529
"href",
@@ -525,15 +544,20 @@ const FormatConverterUI = ({
525544
}}
526545
/>
527546
</div>
547+
{/* Draw Size Buttons */}
548+
<div className="p-3">
549+
{["25%", "50%", "75%", "100%"].map((size) => (
550+
<Button
551+
text={size}
552+
minimal={drawerSize !== size}
553+
onClick={() => setDrawerSize(size)}
554+
/>
555+
))}
556+
</div>
528557
</div>
529-
<div style={{ marginLeft: 10, marginRight: 10 }}>
558+
<div className="mx-3 h-full">
530559
<textarea
531-
style={{
532-
fontFamily: "monospace",
533-
width: "100%",
534-
height: "300px",
535-
overflow: "auto",
536-
}}
560+
className="p-2 w-full h-full overflow-auto font-mono"
537561
value={displayValue}
538562
onChange={(e) => setDisplayValue(e.target.value)}
539563
/>

0 commit comments

Comments
 (0)