308 lines
10 KiB
HTML
308 lines
10 KiB
HTML
<!doctype html>
|
|
<html>
|
|
<head>
|
|
<style>
|
|
:not(:defined) {
|
|
visibility: hidden;
|
|
}
|
|
|
|
body {
|
|
font: 12px var(--sl-font-mono);
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
}
|
|
|
|
sl-input {
|
|
margin-bottom: 10px;
|
|
}
|
|
|
|
sl-button {
|
|
margin-top: 10px;
|
|
}
|
|
|
|
sl-alert {
|
|
margin-top: 20px;
|
|
}
|
|
|
|
sl-tree {
|
|
margin-top: 20px;
|
|
}
|
|
|
|
sl-icon[name="type"] {
|
|
color: var(--sl-color-danger-500);
|
|
}
|
|
|
|
sl-icon[name="square"] {
|
|
color: var(--sl-color-warning-500);
|
|
cursor: pointer;
|
|
}
|
|
|
|
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="icon" type="image/svg+xml" href="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHN2ZyBpZD0iTGF5ZXJfMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB2ZXJzaW9uPSIxLjEiIHZpZXdCb3g9IjAgMCAxMjAwIDEyMDAiPgogIDwhLS0gR2VuZXJhdG9yOiBBZG9iZSBJbGx1c3RyYXRvciAyOS4xLjAsIFNWRyBFeHBvcnQgUGx1Zy1JbiAuIFNWRyBWZXJzaW9uOiAyLjEuMCBCdWlsZCAxNDIpICAtLT4KICA8ZGVmcz4KICAgIDxzdHlsZT4KICAgICAgLnN0MCB7CiAgICAgICAgZmlsbDogI2Y4OTUxZDsKICAgICAgfQoKICAgICAgLnN0MCwgLnN0MSB7CiAgICAgICAgZmlsbC1ydWxlOiB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M3LjYtMzcyIi8+Cjwvc3ZnPg==">
|
|
<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();
|
|
}
|
|
|
|
async function setFromInputs() {
|
|
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');
|
|
|
|
await set(short, long);
|
|
}
|
|
|
|
async function set(short, long) {
|
|
if (short != '') {
|
|
setShortItem(short, 'check-square-fill');
|
|
}
|
|
|
|
const params = new URLSearchParams();
|
|
params.set('short', short);
|
|
params.set('long', long);
|
|
|
|
const oldShort = document.getElementById('short').value;
|
|
const oldLong = document.getElementById('long').value;
|
|
|
|
let resp;
|
|
|
|
try {
|
|
resp = await fetch(`./?${params.toString()}`, {
|
|
method: 'POST',
|
|
});
|
|
} catch (err) {
|
|
console.log(err);
|
|
setTimeout(async () => await set(short, long), 5000);
|
|
return;
|
|
}
|
|
|
|
if (resp.status !== 200) {
|
|
error('Failed to set', (await resp.json()).message);
|
|
return;
|
|
}
|
|
|
|
const data = await resp.json();
|
|
const newShort = data.short;
|
|
|
|
setShortItem(newShort, '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');
|
|
}
|
|
|
|
// Only set the clipboard if the user didn't change the inputs
|
|
if (document.getElementById('short').value == oldShort && document.getElementById('long').value == oldLong) {
|
|
try {
|
|
await navigator.clipboard.writeText(`${window.location.origin}/${newShort}`);
|
|
} catch (err) {
|
|
console.log(err);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
fetch(`./?${suggestParams.toString()}`, {
|
|
method: 'QUERY',
|
|
}).then(async (resp) => {
|
|
for (const short of (await resp.json()).shorts) {
|
|
appendShortItem(short, long);
|
|
}
|
|
}).catch((err) => {});
|
|
}
|
|
|
|
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));
|
|
item.addEventListener('click', () => {
|
|
navigator.clipboard.writeText(`${window.location.origin}/${short}`);
|
|
});
|
|
|
|
tree.insertBefore(item, tree.firstChild);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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'),
|
|
]);
|
|
|
|
document.getElementById('short').setAttribute('label', `${window.location.host}/`);
|
|
|
|
document.getElementById('short').addEventListener('sl-input', () => {
|
|
clearAlerts();
|
|
setInputIcons();
|
|
});
|
|
|
|
document.getElementById('short').addEventListener('keydown', async (e) => {
|
|
if (e.key === 'Enter') {
|
|
await setFromInputs();
|
|
}
|
|
});
|
|
|
|
document.getElementById('short').addEventListener('paste', async () => {
|
|
if (document.getElementById('long').value != '') {
|
|
setTimeout(async () => await setFromInputs(), 0);
|
|
}
|
|
});
|
|
|
|
document.getElementById('short-icon').addEventListener('click', async () => {
|
|
await setFromInputs();
|
|
});
|
|
|
|
|
|
document.getElementById('long').addEventListener('sl-input', () => {
|
|
clearAlerts();
|
|
setInputIcons();
|
|
});
|
|
|
|
document.getElementById('long').addEventListener('keydown', async (e) => {
|
|
if (e.key === 'Enter') {
|
|
await setFromInputs();
|
|
} else {
|
|
document.getElementById('tree').replaceChildren();
|
|
}
|
|
});
|
|
|
|
document.getElementById('long').addEventListener('paste', () => {
|
|
if (document.getElementById('short').value != '') {
|
|
setTimeout(async () => await setFromInputs(), 0);
|
|
}
|
|
});
|
|
|
|
document.getElementById('long-icon').addEventListener('click', async () => {
|
|
await setFromInputs();
|
|
});
|
|
|
|
|
|
document.getElementById('set').addEventListener('click', async () => {
|
|
await setFromInputs();
|
|
});
|
|
|
|
|
|
document.getElementById('long').focus();
|
|
setInputIcons();
|
|
});
|
|
</script>
|
|
</head>
|
|
<body>
|
|
<div id="container" style="width: min(500px, calc(100vw - 10px))">
|
|
<sl-input id="short" value="{{ .path }}">
|
|
<sl-icon id="short-icon" name="type" slot="suffix"></sl-icon>
|
|
</sl-input>
|
|
|
|
<sl-input id="long" label="⟶" type="url">
|
|
<sl-icon id="long-icon" name="type" slot="suffix"></sl-icon>
|
|
</sl-input>
|
|
|
|
<div style="text-align: center; margin-top: 10px;">
|
|
<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>
|