Skip to content

Commit

Permalink
Better again...
Browse files Browse the repository at this point in the history
  • Loading branch information
PzYon committed Jul 13, 2024
1 parent 41f0d71 commit fb3d6d5
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 34 deletions.
97 changes: 69 additions & 28 deletions app/src/components/details/scraps/ScrapContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,66 @@ export const ScrapContextProvider: React.FC<{
setNotes(currentScrap.notes);
}

function changeScrapTypeInternal(
genericNotes: string[],
targetType: ScrapType,
) {
setAppAlert({
message: (
<>
<div>
Would you like to change the type? Certain formatting might be lost.
</div>
<div style={{ margin: "8px 0" }}>
<Button
sx={{
color: "common.white",
border: "1px solid white;",
marginRight: "10px",
}}
variant={"outlined"}
onClick={() => {
const newNotes = convertNotesToTargetType(
targetType,
genericNotes,
);

setNotes(newNotes);

setScrapToRender({
...scrapToRender,
notes: newNotes,
scrapType: targetType,
});

setAppAlert(null);
}}
>
YES
</Button>

<Button
sx={{
color: "common.white",
border: "1px solid white;",
paddingRight: "10px",
}}
variant={"outlined"}
onClick={() => {
setAppAlert(null);
}}
>
NO
</Button>
</div>
</>
),
type: "info",
hideDurationSec: 2,
title: "Change scrap type to " + targetType,
});
}

const contextValue = useMemo<IScrapContext>(
() => {
return {
Expand Down Expand Up @@ -200,21 +260,7 @@ export const ScrapContextProvider: React.FC<{
giveFocus,
hasTitleFocus,
setHasTitleFocus,
changeScrapType: (genericNotes, targetType) => {
console.log("change to " + targetType, genericNotes);

const newNotes = toTargetType(targetType, genericNotes);

console.log("new notes:", newNotes);

setNotes(newNotes);

setScrapToRender({
...scrapToRender,
notes: newNotes,
scrapType: targetType,
});
},
changeScrapType: changeScrapTypeInternal,
};
},
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down Expand Up @@ -278,21 +324,16 @@ export const ScrapContextProvider: React.FC<{
);
};

function toList(genericNotes: string[]) {
return JSON.stringify(
genericNotes.map((n) => ({ label: n }) as IScrapListItem),
);
}

function toMarkdown(genericNotes: string[]) {
return genericNotes.map((n) => "- " + n).join("\n");
}

function toTargetType(targetType: ScrapType, genericNotes: string[]) {
function convertNotesToTargetType(
targetType: ScrapType,
genericNotes: string[],
) {
switch (targetType) {
case ScrapType.List:
return toList(genericNotes);
return JSON.stringify(
genericNotes.map((n) => ({ label: n }) as IScrapListItem),
);
case ScrapType.Markdown:
return toMarkdown(genericNotes);
return genericNotes.map((n) => "- " + n).join("\n");
}
}
6 changes: 3 additions & 3 deletions app/src/components/details/scraps/list/ScrapList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import { useScrapContext } from "../ScrapContext";
import { ScrapBody } from "../ScrapBody";
import {
AddOutlined,
AutoFixHigh,
MoveDownOutlined,
RemoveCircleOutline,
StarHalf,
SyncAltOutlined,
} from "@mui/icons-material";
import { IAction } from "../../../common/actions/IAction";
Expand Down Expand Up @@ -142,8 +142,8 @@ export const ScrapList: React.FC = () => {
);
},
key: "toggle-type",
icon: <StarHalf />,
label: "Toggle type",
icon: <AutoFixHigh fontSize="small" />,
label: "Change type to markdown",
},
{
key: "add",
Expand Down
6 changes: 3 additions & 3 deletions app/src/components/details/scraps/markdown/ScrapMarkdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ActionFactory } from "../../../common/actions/ActionFactory";
import { ScrapBody } from "../ScrapBody";
import { useAppContext } from "../../../../AppContext";
import { useScrapContext } from "../ScrapContext";
import { StarHalf } from "@mui/icons-material";
import { AutoFixHigh } from "@mui/icons-material";
import { ScrapType } from "../../../../serverApi/IScrapEntry";
import { getRawRowValues } from "./getRawRowValues";

Expand All @@ -31,8 +31,8 @@ export const ScrapMarkdown: React.FC = () => {
changeScrapType(getRawRowValues(notes), ScrapType.List);
},
key: "toggle-type",
icon: <StarHalf />,
label: "Toggle type",
icon: <AutoFixHigh fontSize="small" />,
label: "Change type to list",
},
]}
actions={[ActionFactory.copyValueToClipboard(notes, setAppAlert)]}
Expand Down

0 comments on commit fb3d6d5

Please sign in to comment.