Files
x/static/index.html

361 lines
9.4 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" />
2024-12-04 21:37:23 -08:00
<link rel="icon" type="image/png" href="_favicon.png" />
<link rel="apple-touch-icon" href="_favicon.png" />
2024-11-28 08:15:31 -06:00
<link
2024-11-26 21:38:49 -06:00
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>
let title = '{{ .title }}';
2024-11-26 21:38:49 -06:00
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();
}
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
await set(short, long);
2024-11-27 22:29:03 -06:00
}
async function set(short, long) {
2024-11-26 22:20:10 -06:00
if (short != '') {
setShortItem(short, null, 'check-square-fill');
2024-11-26 22:20:10 -06:00
}
2024-11-26 21:38:49 -06:00
2024-11-27 22:36:00 -06:00
const oldShort = document.getElementById('short').value;
const oldLong = document.getElementById('long').value;
let resp;
2024-11-26 21:38:49 -06:00
try {
2024-12-06 13:07:08 -08:00
console.log(short, long);
resp = await fetch('./', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
short: short,
long: long,
}),
});
} catch (err) {
console.log(err);
setTimeout(async () => await set(short, long), 5000);
return;
}
2024-11-26 22:20:10 -06:00
if (resp.status !== 200) {
error('Failed to set', (await resp.json()).message);
return;
}
2024-11-27 22:29:03 -06:00
const data = await resp.json();
const newShort = data.short;
2024-12-03 20:33:57 -08:00
const newURL = data.url;
2024-12-03 20:33:57 -08:00
setShortItem(newShort, newURL, 'check-square');
// Only set the icons if we were actually setting from these inputs
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) {
try {
2024-12-03 20:33:57 -08:00
await navigator.clipboard.writeText(newURL);
} catch (err) {
console.log(err);
2024-11-27 22:29:03 -06:00
}
}
2024-11-27 22:29:03 -06:00
const shorts = [];
2024-11-27 22:29:03 -06:00
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') {
shorts.push(elem.textContent);
2024-11-27 22:29:03 -06:00
}
2024-11-26 21:38:49 -06:00
}
2024-11-27 22:29:03 -06:00
suggest(shorts, title);
}
async function suggest(shorts, title) {
try {
resp = await fetch('./', {
method: 'QUERY',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
shorts: shorts,
title: title,
}),
});
} catch (err) {
console.log(err);
return;
}
const data = await resp.json();
for (const short of data.shorts) {
appendShortItem(short, long);
}
2024-11-26 21:38:49 -06:00
}
2024-12-03 20:33:57 -08:00
function setShortItem(short, url, icon) {
2024-11-26 21:38:49 -06:00
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-12-03 20:33:57 -08:00
if (url != null) {
item.addEventListener('click', () => {
2024-12-03 20:33:57 -08:00
navigator.clipboard.writeText(url);
});
}
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-12-02 20:45:54 -08:00
let shortPaste = false;
document.getElementById('short').addEventListener('sl-input', async () => {
2024-11-26 21:38:49 -06:00
clearAlerts();
setInputIcons();
2024-12-02 20:45:54 -08:00
if (shortPaste) {
shortPaste = false;
await setFromInputs();
}
2024-11-26 21:38:49 -06:00
});
document.getElementById('short').addEventListener('keydown', async (e) => {
2024-11-26 21:38:49 -06:00
if (e.key === 'Enter') {
await setFromInputs();
2024-11-26 21:38:49 -06:00
}
});
document.getElementById('short').addEventListener('paste', async () => {
2024-11-26 21:38:49 -06:00
if (document.getElementById('long').value != '') {
2024-12-02 20:45:54 -08:00
shortPaste = true;
2024-11-26 21:38:49 -06:00
}
});
document.getElementById('short-icon').addEventListener('click', async () => {
await setFromInputs();
2024-11-26 21:38:49 -06:00
});
2024-12-02 20:45:54 -08:00
let longPaste = false;
2024-11-26 21:38:49 -06:00
2024-12-02 20:45:54 -08:00
document.getElementById('long').addEventListener('sl-input', async () => {
2024-11-26 21:38:49 -06:00
clearAlerts();
setInputIcons();
2024-12-03 13:26:20 -08:00
document.getElementById('tree').replaceChildren();
title = '';
2024-12-02 20:45:54 -08:00
if (longPaste) {
longPaste = false;
await setFromInputs();
}
2024-11-26 21:38:49 -06:00
});
document.getElementById('long').addEventListener('keydown', async (e) => {
2024-11-26 21:38:49 -06:00
if (e.key === 'Enter') {
await setFromInputs();
2024-11-26 21:38:49 -06:00
}
});
document.getElementById('long').addEventListener('paste', () => {
if (document.getElementById('short').value != '') {
2024-12-02 20:45:54 -08:00
longPaste = true;
2024-11-26 21:38:49 -06:00
}
});
document.getElementById('long-icon').addEventListener('click', async () => {
await setFromInputs();
2024-11-26 21:38:49 -06:00
});
document.getElementById('set').addEventListener('click', async () => {
await setFromInputs();
2024-11-26 21:38:49 -06:00
});
2024-12-05 17:26:00 -08:00
if (document.getElementById('long').value == '') {
document.getElementById('long').focus();
} else {
document.getElementById('short').focus();
}
2024-11-26 21:38:49 -06:00
2024-11-26 22:07:05 -06:00
setInputIcons();
const short = document.getElementById('short').value;
if (short != '') {
suggest([short], title);
} else if (title != '') {
suggest([], title);
}
2024-11-26 21:38:49 -06:00
});
</script>
</head>
<body>
2024-12-05 00:07:04 -08:00
<div id="container" style="width: min(500px, calc(100vw - 10px))">
<sl-input id="short" value="{{ .short }}" label="{{ .host }}/">
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-12-02 22:29:01 -08:00
<sl-input id="long" value="{{ .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>