Skip to content

Commit

Permalink
fix: use Lit for components #336 (#338)
Browse files Browse the repository at this point in the history
* feat: create settings-item components

* feat: create settings-separator components

* fix: refactor

* fix: delete unused exports

* fix: refactor

* [WIP] feat: create sbs-message component

* fix: extract asyncQuerySelector() as utility

* feat: use sbs-message element for TweetChecker class

* fix: fix layout bug

* fix: add TODO comment

* feat: use sbs-message element for ProfileChecker class

* chore: tempolary delete sbsMessageWrapper.ts

* chore: rename message.ts to sbsMessageWrapper.ts

* chore: restore sbsMessageWrapper.ts content

* fix: delete unused file and variables

* fix: delete unused constant

* fix: fix text color of the buttons in the sbs-messages

* fix: replace asyncQuerySelector() with async-query

* fix: inherit font family from parent element

* fix: sbsMessage is not imported in user scripts
  • Loading branch information
Robot-Inventor authored Nov 16, 2023
1 parent 77a6cff commit 25c7694
Show file tree
Hide file tree
Showing 29 changed files with 1,263 additions and 522 deletions.
57 changes: 32 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@
"@typescript-eslint/eslint-plugin": "^6.2.0",
"@typescript-eslint/parser": "^6.2.0",
"addons-linter": "^6.15.0",
"async-query": "^1.0.0",
"colors": "^1.4.0",
"copy-webpack-plugin": "^11.0.0",
"css-loader": "^6.8.1",
"eslint": "^8.46.0",
"eslint-config-prettier": "^9.0.0",
"glob": "^10.3.4",
"lit": "^3.0.2",
"prettier": "^3.0.0",
"style-loader": "^3.3.3",
"ts-auto-guard": "^4.1.3",
Expand Down
53 changes: 1 addition & 52 deletions src/css/browserAction.css
Original file line number Diff line number Diff line change
Expand Up @@ -67,61 +67,10 @@ fieldset {
border: none;
}

.settings-separator {
--left-line-width: 0.75rem;

opacity: 0.7;
font-size: 0.9em;
position: relative;
left: var(--left-line-width);
}

.settings-separator:not(:first-of-type) {
settings-separator:not(:first-of-type) {
margin-top: 1rem;
}

.settings-separator > hr {
border: none;
width: calc(100vw - var(--window-padding) * 2);
height: 0.1rem;
background: var(--theme-font-color);
position: absolute;
top: 50%;
transform: translate(calc(-1 * var(--left-line-width)), -50%);
z-index: 1;
}

.settings-separator > div {
width: max-content;
background: var(--theme-background-color);
position: relative;
z-index: 2;
padding: 0 0.5rem;
}

.settings-item {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
margin: 0.5rem 0;
padding: 0 1rem;
}

.settings-item,
.settings-item > * {
cursor: pointer;
}

.settings-item > label {
flex-grow: 1;
}

md-checkbox {
margin-left: 0.75rem;
flex-shrink: 0;
}

small {
font-size: 0.8em;
opacity: 0.7;
Expand Down
52 changes: 1 addition & 51 deletions src/html/browserAction.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,7 @@ <h1>
></a>
</h1>
<fieldset>
<div class="settings-separator">
<hr />
<div data-sb-translation="settingsWhereToDisplayCheckResults"></div>
</div>
<div class="settings-item">
<label
for="showMessagesInUnproblematicTweets"
data-sb-translation="settingsShowMessagesInUnproblematicTweets"
></label>
<md-checkbox
name="showMessagesInUnproblematicTweets"
id="showMessagesInUnproblematicTweets"
></md-checkbox>
</div>
<div class="settings-item">
<label
for="showMessagesInUnproblematicProfiles"
data-sb-translation="settingsShowMessagesInUnproblematicProfiles"
></label>
<md-checkbox
name="showMessagesInUnproblematicProfiles"
id="showMessagesInUnproblematicProfiles"
></md-checkbox>
</div>
<div class="settings-item">
<label for="enableForOtherUsersTweets" data-sb-translation="settingsEnableForOtherUsersTweets"></label>
<md-checkbox name="enableForOtherUsersTweets" id="enableForOtherUsersTweets"></md-checkbox>
</div>
<div class="settings-item">
<label
for="enableForOtherUsersProfiles"
data-sb-translation="settingsEnableForOtherUsersProfiles"
></label>
<md-checkbox name="enableForOtherUsersProfiles" id="enableForOtherUsersProfiles"></md-checkbox>
</div>
<div class="settings-separator">
<hr />
<div data-sb-translation="settingsOtherSettings"></div>
</div>
<div class="settings-item">
<label for="alwaysDetailedView" data-sb-translation="settingsAlwaysDetailedView"></label>
<md-checkbox name="alwaysDetailedView" id="alwaysDetailedView"></md-checkbox>
</div>
<div class="settings-item">
<label for="showNotesInMessages" data-sb-translation="settingsShowNotesInMessages"></label>
<md-checkbox name="showNotesInMessages" id="showNotesInMessages"></md-checkbox>
</div>
<div class="settings-item">
<label for="showTweetButton" data-sb-translation="settingsShowTweetButton"></label>
<md-checkbox name="showTweetButton" id="showTweetButton"></md-checkbox>
</div>
<!-- The content will be generated automatically. -->
</fieldset>
<small data-sb-translation="settingsPleaseReload"></small>
<small id="footer-links">
Expand Down
29 changes: 0 additions & 29 deletions src/ts/browserAction.ts

This file was deleted.

5 changes: 5 additions & 0 deletions src/ts/browserAction/browserAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { loadSettings } from "./loadSettings";
import { loadVersion } from "./loadVersion";

void loadSettings();
loadVersion();
96 changes: 96 additions & 0 deletions src/ts/browserAction/loadSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import "../components/settingsItem";
import "../components/settingsSeparator";
import { DEFAULT_SETTINGS } from "../common/defaultSettings";
import { SETTINGS_ITEMS } from "./settingsItems";
import { Settings } from "../@types/common/settings";
import { TRANSLATION_ATTRIBUTE } from "../common/constants";
import { Translator } from "../common/translator";
import browser from "webextension-polyfill";
import { isSettings } from "../@types/common/settings.guard";

/**
* Load settings from local storage.
* @returns settings
*/
const loadSettingsFromStorage = async () => {
const settings = await browser.storage.local.get(DEFAULT_SETTINGS);
if (!isSettings(settings)) throw new Error("Failed to get settings from storage");

return settings;
};

/**
* Create new settings separator element.
* @param translationKey translation key
* @returns created settings separator
*/
const createSettingsSeparator = (translationKey: string) => {
const separator = document.createElement("settings-separator");
separator.setAttribute(TRANSLATION_ATTRIBUTE, translationKey);

return separator;
};

/**
* Create new settings item element.
* @param settingsName settings name
* @param translationKey translation key
* @param checked settings status
* @returns created settings item
*/
const createSettingsItem = (settingsName: string, translationKey: string, checked: boolean) => {
const item = document.createElement("settings-item");
item.settingsName = settingsName;
item.setAttribute(TRANSLATION_ATTRIBUTE, translationKey);
item.checked = checked;
item.addEventListener("change", () => {
void browser.storage.local.set({ [item.settingsName]: item.checked });
});

return item;
};

/**
* Run translation.
*/
const runTranslation = () => {
const translator = new Translator((key) => browser.i18n.getMessage(key), browser.runtime.getURL("dist/image/"));
translator.translateElements();
};

/**
* Create settings item or separator element from data.
* @param settings settings
* @param data settings item data
* @returns created element
*/
const createItemsFromData = (settings: Settings, data: (typeof SETTINGS_ITEMS)[number]) => {
if (data.type === "separator") {
return createSettingsSeparator(data.translationKey);
} else if (data.type === "checkbox") {
return createSettingsItem(data.settingsName, data.translationKey, settings[data.settingsName]);
}

throw new Error("Invalid data");
};

/**
* Load settings UI.
*/
const loadSettings = async () => {
const settings = await loadSettingsFromStorage();

const fieldset = document.querySelector("fieldset");
if (!fieldset) throw new Error("Failed to get fieldset");
const fragment = document.createDocumentFragment();

for (const data of SETTINGS_ITEMS) {
const element = createItemsFromData(settings, data);
fragment.appendChild(element);
}

fieldset.appendChild(fragment);
runTranslation();
};

export { loadSettings };
12 changes: 12 additions & 0 deletions src/ts/browserAction/loadVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import browser from "webextension-polyfill";

const loadVersion = () => {
const { version } = browser.runtime.getManifest();
const versionElement = document.getElementById("version-number");
if (!versionElement) throw new Error("Failed to get #version-number element");

versionElement.textContent = `v${version}`;
versionElement.setAttribute("href", `https://github.com/Robot-Inventor/shadowban-scanner/releases/tag/v${version}`);
};

export { loadVersion };
Loading

0 comments on commit 25c7694

Please sign in to comment.