Files
x/static/index.html

295 lines
8.1 KiB
HTML
Raw Normal View History

2024-11-26 21:38:49 -06:00
<!doctype html>
<html>
<head>
<style>
:not(:defined) {
visibility: hidden;
}
body {
font: 12px var(--sl-font-mono);
display: flex;
flex-direction: column;
align-items: center;
}
2024-11-28 07:44:04 -06:00
sl-input {
margin-bottom: 10px;
}
sl-button {
margin-top: 10px;
}
sl-alert {
margin-top: 20px;
}
sl-tree {
margin-top: 20px;
}
2024-11-26 21:38:49 -06:00
sl-icon[name="type"] {
color: var(--sl-color-danger-500);
}
sl-icon[name="square"] {
color: var(--sl-color-warning-500);
2024-11-26 22:07:05 -06:00
cursor: pointer;
2024-11-26 21:38:49 -06:00
}
sl-icon[name="check-square"] {
color: var(--sl-color-success-500);
}
sl-icon[name="check-square-fill"] {
color: var(--sl-color-success-500);
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link
rel="stylesheet"
media="(prefers-color-scheme:light)"
href="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.18.0/cdn/themes/light.css"
/>
<link
rel="stylesheet"
media="(prefers-color-scheme:dark)"
href="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.18.0/cdn/themes/dark.css"
onload="document.documentElement.classList.add('sl-theme-dark');"
/>
<script type="module" src="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.18.0/cdn/shoelace-autoloader.js"></script>
<script>
function setInputIcon(val, icon) {
if (val.length > 0) {
icon.setAttribute('name', 'square');
} else {
icon.setAttribute('name', 'type');
}
}
function setInputIcons() {
setInputIcon(
document.getElementById('short').value,
document.getElementById('short-icon'),
);
setInputIcon(
document.getElementById('long').value,
document.getElementById('long-icon'),
);
}
function clearAlerts() {
document.getElementById('err').hide();
}
function error(err1, err2) {
clearAlerts();
document.getElementById('err1').innerText = err1;
document.getElementById('err2').innerText = err2;
document.getElementById('err').show();
}
2024-11-27 22:29:03 -06:00
async function setFromInputs() {
2024-11-26 21:38:49 -06:00
const short = document.getElementById('short').value;
const long = document.getElementById('long').value;
if (long == '') {
error('Unable to set', 'Long URL is required');
return;
}
document.getElementById('short-icon').setAttribute('name', 'check-square-fill');
document.getElementById('long-icon').setAttribute('name', 'check-square-fill');
2024-11-26 22:20:10 -06:00
2024-11-27 22:29:03 -06:00
set(short, long);
}
function set(short, long) {
2024-11-26 22:20:10 -06:00
if (short != '') {
setShortItem(short, 'check-square-fill');
}
2024-11-26 21:38:49 -06:00
const params = new URLSearchParams();
params.set('short', short);
params.set('long', long);
2024-11-27 22:36:00 -06:00
const oldShort = document.getElementById('short').value;
const oldLong = document.getElementById('long').value;
2024-11-27 22:29:03 -06:00
fetch(`./?${params.toString()}`, {
2024-11-26 21:38:49 -06:00
method: 'POST',
2024-11-27 22:29:03 -06:00
}).then(async (resp) => {
if (resp.status !== 200) {
error('Failed to set', (await resp.json()).message);
return;
}
2024-11-26 21:38:49 -06:00
2024-11-27 22:29:03 -06:00
const data = await resp.json();
const newShort = data.short;
2024-11-26 22:20:10 -06:00
setShortItem(newShort, 'check-square');
2024-11-27 22:29:03 -06:00
2024-11-27 22:36:00 -06:00
// Only set the icons if we were actually setting from these inputs
2024-11-27 22:29:03 -06:00
if (document.getElementById('short').value == short && document.getElementById('long').value == long) {
document.getElementById('short-icon').setAttribute('name', 'check-square');
document.getElementById('long-icon').setAttribute('name', 'check-square');
2024-11-27 22:36:00 -06:00
}
// Only set the clipboard if the user didn't change the inputs
if (document.getElementById('short').value == oldShort && document.getElementById('long').value == oldLong) {
2024-11-27 22:29:03 -06:00
await navigator.clipboard.writeText(`${window.location.origin}/${newShort}`);
}
});
const suggestParams = new URLSearchParams();
for (const elem of document.getElementById('tree').children) {
const icon = elem.getElementsByTagName('sl-icon')[0];
if (icon.getAttribute('name') == 'check-square-fill' ||
icon.getAttribute('name') == 'check-square') {
suggestParams.append('short', elem.textContent);
}
2024-11-26 21:38:49 -06:00
}
2024-11-27 22:29:03 -06:00
fetch(`./?${suggestParams.toString()}`, {
method: 'QUERY',
}).then(async (resp) => {
for (const short of (await resp.json()).shorts) {
appendShortItem(short, long);
}
});
2024-11-26 21:38:49 -06:00
}
function setShortItem(short, icon) {
const tree = document.getElementById('tree');
for (const item of tree.children) {
if (item.textContent == short) {
tree.removeChild(item);
}
}
const item = document.createElement('sl-tree-item');
item.appendChild(document.createElement('sl-icon')).setAttribute('name', icon);
item.appendChild(document.createTextNode(short));
2024-11-27 22:29:03 -06:00
item.addEventListener('click', () => {
navigator.clipboard.writeText(`${window.location.origin}/${short}`);
});
2024-11-27 16:46:18 -06:00
2024-11-26 21:38:49 -06:00
tree.insertBefore(item, tree.firstChild);
}
2024-11-27 22:29:03 -06:00
function appendShortItem(short, long) {
const tree = document.getElementById('tree');
for (const item of tree.children) {
if (item.textContent == short) {
return;
}
}
const item = document.createElement('sl-tree-item');
item.appendChild(document.createElement('sl-icon')).setAttribute('name', 'square');
item.appendChild(document.createTextNode(short));
item.addEventListener('click', () => {
set(short, long);
});
tree.appendChild(item);
}
2024-11-26 22:07:05 -06:00
document.addEventListener('DOMContentLoaded', async () => {
await Promise.all([
customElements.whenDefined('sl-input'),
customElements.whenDefined('sl-icon'),
customElements.whenDefined('sl-button'),
customElements.whenDefined('sl-alert'),
customElements.whenDefined('sl-tree'),
]);
2024-11-26 21:38:49 -06:00
document.getElementById('short').setAttribute('label', `${window.location.host}/`);
document.getElementById('short').addEventListener('sl-input', () => {
clearAlerts();
setInputIcons();
});
document.getElementById('short').addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
2024-11-27 22:29:03 -06:00
setFromInputs();
2024-11-26 21:38:49 -06:00
}
});
document.getElementById('short').addEventListener('paste', () => {
if (document.getElementById('long').value != '') {
2024-11-27 22:29:03 -06:00
setTimeout(() => setFromInputs(), 0);
2024-11-26 21:38:49 -06:00
}
});
document.getElementById('short-icon').addEventListener('click', () => {
2024-11-27 22:29:03 -06:00
setFromInputs();
2024-11-26 21:38:49 -06:00
});
document.getElementById('long').addEventListener('sl-input', () => {
clearAlerts();
setInputIcons();
});
document.getElementById('long').addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
2024-11-27 22:29:03 -06:00
setFromInputs();
2024-11-26 21:38:49 -06:00
} else {
document.getElementById('tree').replaceChildren();
}
});
document.getElementById('long').addEventListener('paste', () => {
if (document.getElementById('short').value != '') {
2024-11-27 22:29:03 -06:00
setTimeout(() => setFromInputs(), 0);
2024-11-26 21:38:49 -06:00
}
});
document.getElementById('long-icon').addEventListener('click', () => {
2024-11-27 22:29:03 -06:00
setFromInputs();
2024-11-26 21:38:49 -06:00
});
document.getElementById('set').addEventListener('click', () => {
2024-11-27 22:29:03 -06:00
setFromInputs();
2024-11-26 21:38:49 -06:00
});
document.getElementById('long').focus();
2024-11-26 22:07:05 -06:00
setInputIcons();
2024-11-26 21:38:49 -06:00
});
</script>
</head>
<body>
<div id="container" style="width: min(500px, calc(100vw - 10px))">
2024-11-26 22:07:05 -06:00
<sl-input id="short" value="{{ .path }}">
2024-11-26 21:38:49 -06:00
<sl-icon id="short-icon" name="type" slot="suffix"></sl-icon>
</sl-input>
2024-11-28 07:44:04 -06:00
2024-11-27 21:36:20 -06:00
<sl-input id="long" label="⟶" type="url">
2024-11-26 21:38:49 -06:00
<sl-icon id="long-icon" name="type" slot="suffix"></sl-icon>
</sl-input>
2024-11-28 07:44:04 -06:00
<div style="text-align: center; margin-top: 10px;">
2024-11-26 21:38:49 -06:00
<sl-button variant="primary" id="set">Set</sl-button>
</div>
<sl-alert id="err" variant="danger">
<sl-icon slot="icon" name="exclamation-octagon"></sl-icon>
<strong id="err1"></strong><br />
<span id="err2"></span>
</sl-alert>
<sl-tree id="tree">
</sl-tree>
</div>
</body>
</html>