Skip to content

Commit

Permalink
#212 Move some files around for cohesion
Browse files Browse the repository at this point in the history
  • Loading branch information
braxtonhall committed Jan 12, 2023
1 parent 40d78cb commit ce88e0f
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import {getTagsFromElement, getTagTrees, TagTrees} from "../../adapters/tags";
import {createModal} from "../../../common/ui/modal";
import {loaderOverlaid} from "../../../common/ui/loadingIndicator";
import {UIColour} from "../../../common/ui/colour";
import {OffSave, OnSave} from "../../entities/bookForm";
import {Highlight, Highlightable, highlighted} from "../../../common/ui/highlighter";
import {getSheetLink} from "../../../common/entities/spreadsheet";
import {insertTagsDialogs} from "../../dialogs/insertTags";

type GetTagsOptions = {noCache: boolean};
import {getTagsFromElement, getTagTrees, TagTrees} from "../../../adapters/tags";
import {loaderOverlaid} from "../../../../common/ui/loadingIndicator";
import {OffSave, OnSave} from "../../../entities/bookForm";
import {Highlight, Highlightable, highlighted} from "../../../../common/ui/highlighter";
import {getUserAcceptance} from "./dialogs/userAcceptance";
import {GetTagsOptions} from "./types";

const applyHighlights = async (text: string): Promise<Highlight[]> => {
const validTags = await getTagTrees().catch(() => new Map());
Expand All @@ -33,56 +29,6 @@ const applyHighlights = async (text: string): Promise<Highlight[]> => {
const getInvalidTags = (tags: string[], trees: TagTrees): string[] =>
tags.filter((tag) => !trees.has(tag.toLowerCase()));

const getUserAcceptance = (
invalidTags: string[],
saveHandler: (options: GetTagsOptions) => Promise<boolean>
): Promise<boolean> =>
new Promise<boolean>((resolve) =>
createModal({
text: "Are you sure? The following tags are not in the Tag Index",
subText: invalidTags,
elements: [
{
kind: "button",
text: "Open the Tag Index",
colour: UIColour.GREY,
onClick: async () => resolve(getSecondaryAcceptance(saveHandler)),
},
{
kind: "button",
text: "Insert Tags",
colour: UIColour.GREY,
onClick: async () =>
resolve(insertTagsDialogs(invalidTags).then(() => saveHandler({noCache: true}))),
},
{kind: "button", text: "Save anyway", colour: UIColour.GREY, onClick: async () => resolve(true)},
{kind: "button", text: "Cancel", colour: UIColour.PURPLE, onClick: async () => resolve(false)},
],
colour: UIColour.PURPLE,
onCancel: async () => resolve(false),
})
);

const getSecondaryAcceptance = async (saveHandler: (options: GetTagsOptions) => Promise<boolean>): Promise<boolean> => {
window.open(await getSheetLink());
return new Promise<boolean>((resolve) =>
createModal({
text: "Did you put the new tags in the Tag Index?",
elements: [
{
kind: "button",
text: "Yes!",
colour: UIColour.GREY,
onClick: async () => resolve(saveHandler({noCache: true})),
},
{kind: "button", text: "Cancel", colour: UIColour.PURPLE, onClick: async () => resolve(false)},
],
colour: UIColour.PURPLE,
onCancel: async () => resolve(false),
})
);
};

const fixTagsCase = (tags: string[], trees: TagTrees): string[] =>
tags.map((tag) => trees.get(tag.toLowerCase())?.tag ?? tag);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {createModal} from "../../common/ui/modal";
import {UIColour} from "../../common/ui/colour";
import {UIColour} from "../../../../../common/ui/colour";
import {createModal} from "../../../../../common/ui/modal";

const toInsertionButton = (remainingTags: string[], resolve: (value) => void) => (tag: string) => ({
kind: "button" as const,
colour: UIColour.BLUE,
text: tag,
onClick: async () => resolve(insertTag(tag, remainingTags).then(insertTagsDialogs)),
onClick: async () => resolve(insertTag(tag, remainingTags).then(insertTags)),
});

const insertTagsDialogs = async (tags: string[]): Promise<boolean> => {
const insertTags = async (tags: string[]): Promise<boolean> => {
if (tags.length === 0) {
return Promise.resolve(true);
} else {
Expand Down Expand Up @@ -44,4 +44,4 @@ const insertTag = (tag: string, remainingTags: string[]): Promise<string[]> =>
})
);

export {insertTagsDialogs};
export {insertTags};
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {createModal} from "../../../../../common/ui/modal";
import {UIColour} from "../../../../../common/ui/colour";
import {insertTags} from "./insertTags";
import {getSheetLink} from "../../../../../common/entities/spreadsheet";
import {GetTagsOptions} from "../types";

const getUserAcceptance = (
invalidTags: string[],
saveHandler: (options: GetTagsOptions) => Promise<boolean>
): Promise<boolean> =>
new Promise<boolean>((resolve) =>
createModal({
text: "Are you sure? The following tags are not in the Tag Index",
subText: invalidTags,
elements: [
{
kind: "button",
text: "Open the Tag Index",
colour: UIColour.GREY,
onClick: async () => resolve(getSecondaryAcceptance(saveHandler)),
},
{
kind: "button",
text: "Insert Tags",
colour: UIColour.GREY,
onClick: async () => resolve(insertTags(invalidTags).then(() => saveHandler({noCache: true}))),
},
{kind: "button", text: "Save anyway", colour: UIColour.GREY, onClick: async () => resolve(true)},
{kind: "button", text: "Cancel", colour: UIColour.PURPLE, onClick: async () => resolve(false)},
],
colour: UIColour.PURPLE,
onCancel: async () => resolve(false),
})
);

const getSecondaryAcceptance = async (saveHandler: (options: GetTagsOptions) => Promise<boolean>): Promise<boolean> => {
window.open(await getSheetLink());
return new Promise<boolean>((resolve) =>
createModal({
text: "Did you put the new tags in the Tag Index?",
elements: [
{
kind: "button",
text: "Yes!",
colour: UIColour.GREY,
onClick: async () => resolve(saveHandler({noCache: true})),
},
{kind: "button", text: "Cancel", colour: UIColour.PURPLE, onClick: async () => resolve(false)},
],
colour: UIColour.PURPLE,
onCancel: async () => resolve(false),
})
);
};

export {getUserAcceptance};
3 changes: 3 additions & 0 deletions src/ts/content/extensions/util/tagValidation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {appendTagValidator} from "./appendTagValidator";

export {appendTagValidator};
3 changes: 3 additions & 0 deletions src/ts/content/extensions/util/tagValidation/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type GetTagsOptions = {noCache: boolean};

export type {GetTagsOptions};

0 comments on commit ce88e0f

Please sign in to comment.