-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
125 additions
and
103 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,69 @@ | ||
/** | ||
* Retrieves an element from the DOM based on the provided element and include selectors. | ||
* @template T | ||
* @typedef {new (...args: any[]) => T} Class<T> | ||
*/ | ||
|
||
/** | ||
* Retrieves an element from the DOM based on the provided element and include selectors, and type. | ||
* | ||
* @overload | ||
* @param {string} selector | ||
* @param {string} includeSelector | ||
* @returns {Promise<Element | null>} | ||
*/ | ||
/** | ||
* @template E | ||
* @overload | ||
* @param {string} selector | ||
* @param {string} includeSelector | ||
* @param {Class<E> =} type | ||
* @returns {Promise<ReturnType<typeof typedQuerySelector<E>> | null>} | ||
* | ||
*/ | ||
/** | ||
* @param {string} selector - The CSS selector of the element to retrieve. | ||
* @param {string} includeSelector - The CSS selector of the sl-include element. | ||
* @returns {Promise<Element|null>} A promise that resolves to the found element or null if not found. | ||
* @param {Class<E> =} type - The expected type of the element. | ||
*/ | ||
function getIncludedElement(selector, includeSelector) { | ||
function getIncludedElement(selector, includeSelector, type) { | ||
return new Promise((resolve) => { | ||
const includeElement = document.querySelector(includeSelector); | ||
if (!includeElement) { | ||
return resolve(null); | ||
} | ||
|
||
const element = includeElement.querySelector(selector); | ||
const getElement = () => | ||
type | ||
? typedQuerySelector(selector, type, includeElement) | ||
: includeElement.querySelector(selector); | ||
const element = getElement(); | ||
|
||
if (element) { | ||
return resolve(element); | ||
} | ||
|
||
includeElement.addEventListener("sl-load", () => { | ||
const element = document.querySelector(selector); | ||
const element = getElement(); | ||
|
||
resolve(element); | ||
}); | ||
}); | ||
} | ||
|
||
|
||
/** | ||
* @template T | ||
* @typedef {new (...args: any[]) => T} Class<T> | ||
*/ | ||
/** | ||
* Retrieves an element from the DOM based on the provided selector and expected type. | ||
* | ||
* | ||
* @template E | ||
* @param {string} selector | ||
* @param {string} selector | ||
* @param {Class<E>} type | ||
* @param {ParentNode | null | undefined} parent | ||
* @return {E | null} | ||
*/ | ||
function typedQuerySelector(selector, type, parent = document) { | ||
const element = parent?.querySelector(selector); | ||
if(element instanceof type) { | ||
return element | ||
if (element instanceof type) { | ||
return element; | ||
} | ||
|
||
return null | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,74 @@ | ||
function InstanceSave() { | ||
// If save button is clicked | ||
const instanceField = typedQuerySelector("input#InstanceField", HTMLInputElement) | ||
const INSTANCE_STORE_KEY = "Instance"; | ||
|
||
if (instanceField) { | ||
const instance = instanceField.value || ""; | ||
localStorage.setItem("Instance", instance); | ||
} | ||
window.addEventListener("DOMContentLoaded", async () => { | ||
const savedInstance = await registerSavedInstance(); | ||
|
||
const instanceSaveButton = typedQuerySelector("#InstanceSaveButton", HTMLInputElement) | ||
if (instanceSaveButton) { | ||
instanceSaveButton.style.backgroundColor = "#00ff89"; | ||
instanceSaveButton.setAttribute("value", "Saved!"); | ||
setTimeout(() => { | ||
instanceSaveButton.style.backgroundColor = "#575151"; | ||
instanceSaveButton.setAttribute("value", "Save"); | ||
}, 1200); | ||
} | ||
} | ||
setDefaultTab(savedInstance); | ||
openTab(savedInstance); | ||
prepareSaveButton(); | ||
}); | ||
|
||
function InstanceGet() { | ||
// Runs on start | ||
const InstanceStore = localStorage.getItem("Instance"); | ||
async function prepareSaveButton() { | ||
const { instanceSaveButton } = await getInstanceSettingsForm(); | ||
|
||
if (InstanceStore) { | ||
window.api.send("registerInstance", InstanceStore); | ||
setTimeout(() => { | ||
const instanceField = typedQuerySelector("input#InstanceField", HTMLInputElement) | ||
instanceSaveButton?.addEventListener("click", (event) => { | ||
const target = event.target; | ||
const element = target instanceof HTMLElement ? target : null; | ||
saveInstance(element); | ||
}); | ||
} | ||
|
||
if (instanceField) { | ||
instanceField.value = InstanceStore; | ||
} | ||
}, 0o500); | ||
/** | ||
* @param {HTMLElement | null} trigger | ||
*/ | ||
async function saveInstance(trigger) { | ||
const isInputButton = trigger instanceof HTMLInputElement; | ||
const { instanceField } = await getInstanceSettingsForm(); | ||
|
||
const instance = instanceField?.value; | ||
if (instance) { | ||
localStorage.setItem(INSTANCE_STORE_KEY, instance); | ||
} else { | ||
localStorage.removeItem(INSTANCE_STORE_KEY); | ||
} | ||
|
||
if (isInputButton) { | ||
trigger.style.backgroundColor = "#00ff89"; | ||
trigger.setAttribute("value", "Saved!"); | ||
setTimeout(() => { | ||
trigger.style.backgroundColor = "#575151"; | ||
trigger.setAttribute("value", "Save"); | ||
}, 1200); | ||
} | ||
} | ||
|
||
if (!localStorage.getItem("firstTime")) { | ||
localStorage.setItem("Instance", "https://design.penpot.app/"); // If not set, by default on first launch, the app will be blank (to fix issue #3) | ||
localStorage.setItem("firstTime", "true"); | ||
// setTimeout(() => {welcome()}, 2500) | ||
} else { | ||
} | ||
async function registerSavedInstance() { | ||
const savedInstance = localStorage.getItem(INSTANCE_STORE_KEY); | ||
|
||
if (savedInstance) { | ||
const { instanceField } = await getInstanceSettingsForm(); | ||
|
||
setTimeout(() => { | ||
const instanceField = typedQuerySelector("input#InstanceField", HTMLInputElement) | ||
window.api.send("registerInstance", savedInstance); | ||
|
||
if (instanceField) { | ||
instanceField.value = savedInstance; | ||
} | ||
|
||
if (instanceField) { | ||
instanceField.value = localStorage.getItem("Instance") || ""; | ||
return savedInstance; | ||
} | ||
}, 0o500); | ||
} | ||
|
||
setTimeout(async () => { | ||
const tabGroup = await getTabGroup(); | ||
const webview = /** @type {import("electron").WebviewTag | null} */ ( | ||
tabGroup?.shadowRoot?.querySelector("div > div > webview") | ||
async function getInstanceSettingsForm() { | ||
const instanceField = await getIncludedElement( | ||
"#instance-field", | ||
"#include-settings", | ||
HTMLInputElement | ||
); | ||
const instanceSaveButton = await getIncludedElement( | ||
"#instance-save", | ||
"#include-settings", | ||
HTMLInputElement | ||
); | ||
const nav = typedQuerySelector("div > nav", HTMLElement, tabGroup?.shadowRoot) | ||
if (webview?.src === "") { | ||
webview.style.opacity = "0"; | ||
if (nav) { | ||
nav.style.opacity = "0"; | ||
} | ||
|
||
console.log("You need to set an instance."); | ||
|
||
const titlebarButton = typedQuerySelector("body > titlebar > div.actions > div > button:nth-child(2)", HTMLButtonElement); | ||
const tdmWarnings = typedQuerySelector(".tdm-warnings", HTMLElement); | ||
|
||
if (titlebarButton) { | ||
titlebarButton?.click(); | ||
} | ||
|
||
if (tdmWarnings) { | ||
tdmWarnings.style.display = "inherit"; | ||
} | ||
} else { | ||
console.log("An instance is set."); | ||
} | ||
}, 1500); | ||
return { instanceField, instanceSaveButton }; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters