2024-02-08 20:21:57 -05:00
|
|
|
|
import {LitElement, html, css, svg} from '/lit/lit-all.min.js';
|
2023-04-29 12:52:35 -04:00
|
|
|
|
|
2024-01-13 12:40:47 -05:00
|
|
|
|
let cm6;
|
2022-06-18 17:12:38 -04:00
|
|
|
|
let gSocket;
|
|
|
|
|
|
|
|
|
|
let gCurrentFile;
|
|
|
|
|
let gFiles = {};
|
2023-03-21 19:08:04 -04:00
|
|
|
|
let gApp = {files: {}, emoji: '📦'};
|
2022-06-18 17:12:38 -04:00
|
|
|
|
let gEditor;
|
|
|
|
|
let gOriginalInput;
|
|
|
|
|
|
2024-02-24 11:09:34 -05:00
|
|
|
|
let kErrorColor = '#dc322f';
|
|
|
|
|
let kStatusColor = '#fff';
|
2022-06-18 17:12:38 -04:00
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
// Functions that server-side app code can call through the app object.
|
2022-06-18 17:12:38 -04:00
|
|
|
|
const k_api = {
|
|
|
|
|
setDocument: {args: ['content'], func: api_setDocument},
|
|
|
|
|
postMessage: {args: ['message'], func: api_postMessage},
|
|
|
|
|
error: {args: ['error'], func: api_error},
|
|
|
|
|
localStorageSet: {args: ['key', 'value'], func: api_localStorageSet},
|
|
|
|
|
localStorageGet: {args: ['key'], func: api_localStorageGet},
|
2022-07-26 20:27:10 -04:00
|
|
|
|
requestPermission: {args: ['permission', 'id'], func: api_requestPermission},
|
2022-08-14 12:58:26 -04:00
|
|
|
|
print: {args: ['...'], func: api_print},
|
2022-09-14 20:16:37 -04:00
|
|
|
|
setHash: {args: ['hash'], func: api_setHash},
|
2022-06-18 17:12:38 -04:00
|
|
|
|
};
|
2017-05-22 15:38:49 -04:00
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
// TODO(tasiaiso): this is only used once, move it down ?
|
2023-04-29 14:23:08 -04:00
|
|
|
|
const k_global_style = css`
|
|
|
|
|
a:link {
|
|
|
|
|
color: #268bd2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a:visited {
|
|
|
|
|
color: #6c71c4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a:hover {
|
|
|
|
|
color: #859900;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
a:active {
|
|
|
|
|
color: #2aa198;
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* Class that represents the top bar
|
|
|
|
|
*/
|
2023-04-29 14:23:08 -04:00
|
|
|
|
class TfNavigationElement extends LitElement {
|
|
|
|
|
static get properties() {
|
|
|
|
|
return {
|
|
|
|
|
credentials: {type: Object},
|
|
|
|
|
permissions: {type: Object},
|
|
|
|
|
show_permissions: {type: Boolean},
|
|
|
|
|
status: {type: Object},
|
2023-04-29 15:27:00 -04:00
|
|
|
|
spark_lines: {type: Object},
|
2023-06-28 19:00:34 -04:00
|
|
|
|
version: {type: Object},
|
|
|
|
|
show_version: {type: Boolean},
|
2024-04-13 13:22:59 -04:00
|
|
|
|
identity: {type: String},
|
|
|
|
|
identities: {type: Array},
|
2024-04-13 21:51:18 -04:00
|
|
|
|
names: {type: Object},
|
2023-04-29 14:23:08 -04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
this.permissions = {};
|
|
|
|
|
this.show_permissions = false;
|
|
|
|
|
this.status = {};
|
2023-04-29 15:27:00 -04:00
|
|
|
|
this.spark_lines = {};
|
2024-04-13 13:22:59 -04:00
|
|
|
|
this.identities = [];
|
2024-04-13 21:51:18 -04:00
|
|
|
|
this.names = {};
|
2023-04-29 14:23:08 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} event
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2023-04-29 14:23:08 -04:00
|
|
|
|
toggle_edit(event) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
if (editing()) {
|
|
|
|
|
closeEditor();
|
|
|
|
|
} else {
|
|
|
|
|
edit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} key
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2023-04-29 14:23:08 -04:00
|
|
|
|
reset_permission(key) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
send({action: 'resetPermission', permission: key});
|
2023-04-29 14:23:08 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} key
|
|
|
|
|
* @param {*} options
|
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2023-04-29 15:27:00 -04:00
|
|
|
|
get_spark_line(key, options) {
|
|
|
|
|
if (!this.spark_lines[key]) {
|
|
|
|
|
let spark_line = document.createElement('tf-sparkline');
|
|
|
|
|
spark_line.title = key;
|
2024-04-13 16:52:30 -04:00
|
|
|
|
spark_line.classList.add('w3-bar-item');
|
|
|
|
|
spark_line.classList.add('w3-hide-small');
|
|
|
|
|
spark_line.style.paddingRight = '0';
|
2023-04-29 15:27:00 -04:00
|
|
|
|
if (options) {
|
|
|
|
|
if (options.max) {
|
|
|
|
|
spark_line.max = options.max;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.spark_lines[key] = spark_line;
|
|
|
|
|
this.requestUpdate();
|
|
|
|
|
}
|
|
|
|
|
return this.spark_lines[key];
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2023-04-29 14:23:08 -04:00
|
|
|
|
render_login() {
|
|
|
|
|
if (this?.credentials?.session?.name) {
|
2024-04-13 20:07:39 -04:00
|
|
|
|
return html`<a
|
|
|
|
|
class="w3-bar-item w3-right"
|
|
|
|
|
id="login"
|
|
|
|
|
href="/login/logout?return=${url() + hash()}"
|
2024-02-24 11:09:34 -05:00
|
|
|
|
>logout ${this.credentials.session.name}</a
|
|
|
|
|
>`;
|
2023-04-29 14:23:08 -04:00
|
|
|
|
} else {
|
2024-04-13 20:07:39 -04:00
|
|
|
|
return html`<a
|
|
|
|
|
class="w3-bar-item w3-right"
|
|
|
|
|
id="login"
|
|
|
|
|
href="/login?return=${url() + hash()}"
|
2024-02-24 11:09:34 -05:00
|
|
|
|
>login</a
|
|
|
|
|
>`;
|
2023-04-29 14:23:08 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-14 17:47:47 -04:00
|
|
|
|
set_active_identity(id) {
|
|
|
|
|
send({action: 'setActiveIdentity', identity: id});
|
|
|
|
|
this.renderRoot.getElementById('id_dropdown').classList.remove('w3-show');
|
2024-04-13 13:22:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
create_identity(event) {
|
2024-04-13 20:32:17 -04:00
|
|
|
|
if (confirm('Are you sure you want to create a new identity?')) {
|
|
|
|
|
send({action: 'createIdentity'});
|
|
|
|
|
}
|
2024-04-13 13:22:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-14 17:47:47 -04:00
|
|
|
|
toggle_id_dropdown() {
|
|
|
|
|
this.renderRoot.getElementById('id_dropdown').classList.toggle('w3-show');
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-17 12:21:44 -04:00
|
|
|
|
edit_profile() {
|
|
|
|
|
window.location.href = '/~core/ssb/#' + this.identity;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-13 13:22:59 -04:00
|
|
|
|
render_identity() {
|
2024-04-13 21:51:18 -04:00
|
|
|
|
let self = this;
|
2024-04-13 13:22:59 -04:00
|
|
|
|
if (this.identities?.length) {
|
|
|
|
|
return html`
|
|
|
|
|
<link type="text/css" rel="stylesheet" href="/static/w3.css" />
|
2024-04-14 17:47:47 -04:00
|
|
|
|
<div class="w3-dropdown-click w3-right" style="max-width: 100%">
|
|
|
|
|
<button
|
|
|
|
|
class="w3-button w3-rest w3-cyan"
|
|
|
|
|
style="text-overflow: ellipsis; overflow: hidden; white-space: nowrap; max-width: 100%"
|
|
|
|
|
@click=${self.toggle_id_dropdown}
|
|
|
|
|
>
|
2024-04-17 20:56:33 -04:00
|
|
|
|
${self.names[this.identity]}${self.names[this.identity] ===
|
|
|
|
|
this.identity
|
|
|
|
|
? ''
|
|
|
|
|
: html` - ${this.identity}`}
|
|
|
|
|
▾
|
2024-04-14 17:47:47 -04:00
|
|
|
|
</button>
|
2024-04-17 20:56:33 -04:00
|
|
|
|
<div
|
|
|
|
|
id="id_dropdown"
|
|
|
|
|
class="w3-dropdown-content w3-bar-block w3-card-4"
|
|
|
|
|
style="max-width: 100%"
|
|
|
|
|
>
|
|
|
|
|
<button
|
|
|
|
|
class="w3-bar-item w3-button w3-border"
|
|
|
|
|
@click=${() => (window.location.href = '/~core/identity')}
|
|
|
|
|
>
|
|
|
|
|
Manage Identities...
|
|
|
|
|
</button>
|
|
|
|
|
<button
|
|
|
|
|
class="w3-bar-item w3-button w3-border"
|
|
|
|
|
@click=${self.edit_profile}
|
|
|
|
|
>
|
|
|
|
|
Edit Profile...
|
|
|
|
|
</button>
|
2024-04-14 17:47:47 -04:00
|
|
|
|
${this.identities.map(
|
|
|
|
|
(x) => html`
|
|
|
|
|
<button
|
2024-04-17 20:56:33 -04:00
|
|
|
|
class="w3-bar-item w3-button ${x === self.identity
|
|
|
|
|
? 'w3-cyan'
|
|
|
|
|
: ''}"
|
2024-04-14 17:47:47 -04:00
|
|
|
|
@click=${() => self.set_active_identity(x)}
|
|
|
|
|
style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap"
|
|
|
|
|
>
|
|
|
|
|
${self.names[x]}${self.names[x] === x ? '' : html` - ${x}`}
|
|
|
|
|
</button>
|
|
|
|
|
`
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2024-04-13 13:22:59 -04:00
|
|
|
|
`;
|
|
|
|
|
} else {
|
|
|
|
|
return html`
|
|
|
|
|
<link type="text/css" rel="stylesheet" href="/static/w3.css" />
|
|
|
|
|
<button
|
2024-04-13 20:32:17 -04:00
|
|
|
|
id="create_identity"
|
2024-04-13 13:22:59 -04:00
|
|
|
|
@click=${this.create_identity}
|
2024-04-14 08:10:32 -04:00
|
|
|
|
class="w3-button w3-mobile w3-blue w3-right"
|
2024-04-13 20:07:39 -04:00
|
|
|
|
>
|
2024-04-13 13:22:59 -04:00
|
|
|
|
Create an Identity
|
|
|
|
|
</button>
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2023-04-29 14:23:08 -04:00
|
|
|
|
render_permissions() {
|
|
|
|
|
if (this.show_permissions) {
|
|
|
|
|
return html`
|
2024-02-24 11:09:34 -05:00
|
|
|
|
<link type="text/css" rel="stylesheet" href="/static/w3.css" />
|
|
|
|
|
<div
|
|
|
|
|
style="position: absolute; top: 0; padding: 0; margin: 0; z-index: 100; display: flex; justify-content: center; width: 100%"
|
|
|
|
|
>
|
|
|
|
|
<div
|
|
|
|
|
style="background-color: #444; padding: 1em; margin: 0 auto; border-left: 4px solid #fff; border-right: 4px solid #fff; border-bottom: 4px solid #fff"
|
|
|
|
|
>
|
2023-04-29 14:23:08 -04:00
|
|
|
|
<div>This app has the following permissions:</div>
|
2024-02-24 11:09:34 -05:00
|
|
|
|
${Object.keys(this.permissions).map(
|
|
|
|
|
(key) => html`
|
2024-04-13 10:03:06 -04:00
|
|
|
|
<div>
|
|
|
|
|
<span>${key}</span>:
|
|
|
|
|
${this.permissions[key] ? '✅ Allowed' : '❌ Denied'}
|
|
|
|
|
<button
|
|
|
|
|
@click=${() => this.reset_permission(key)}
|
|
|
|
|
class="w3-button w3-red"
|
|
|
|
|
>
|
|
|
|
|
Reset
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
`
|
2024-02-24 11:09:34 -05:00
|
|
|
|
)}
|
|
|
|
|
<button
|
|
|
|
|
@click=${() => (this.show_permissions = false)}
|
|
|
|
|
class="w3-button w3-blue"
|
|
|
|
|
>
|
|
|
|
|
Close
|
|
|
|
|
</button>
|
2023-04-29 14:23:08 -04:00
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-17 20:56:33 -04:00
|
|
|
|
clear_error() {
|
|
|
|
|
this.status = {};
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2023-04-29 14:23:08 -04:00
|
|
|
|
render() {
|
|
|
|
|
let self = this;
|
|
|
|
|
return html`
|
2024-04-13 16:52:30 -04:00
|
|
|
|
<link type="text/css" rel="stylesheet" href="/static/w3.css" />
|
2023-04-29 14:23:08 -04:00
|
|
|
|
<style>
|
2024-02-24 11:09:34 -05:00
|
|
|
|
${k_global_style} .tooltip {
|
2023-12-29 13:12:14 -05:00
|
|
|
|
position: absolute;
|
|
|
|
|
z-index: 1;
|
|
|
|
|
display: none;
|
|
|
|
|
border: 1px solid black;
|
|
|
|
|
padding: 4px;
|
|
|
|
|
color: black;
|
|
|
|
|
background: white;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.tooltip_parent:hover .tooltip {
|
|
|
|
|
display: inline-block;
|
|
|
|
|
}
|
2023-04-29 14:23:08 -04:00
|
|
|
|
</style>
|
2024-04-13 20:07:39 -04:00
|
|
|
|
<div class="w3-black w3-bar">
|
2024-02-24 11:09:34 -05:00
|
|
|
|
<span
|
2024-04-13 16:52:30 -04:00
|
|
|
|
class="w3-bar-item"
|
2024-02-24 11:09:34 -05:00
|
|
|
|
style="cursor: pointer"
|
|
|
|
|
@click=${() => (this.show_version = !this.show_version)}
|
|
|
|
|
>😎</span
|
|
|
|
|
>
|
|
|
|
|
<span
|
2024-04-13 16:52:30 -04:00
|
|
|
|
class="w3-bar-item"
|
2024-04-13 20:07:39 -04:00
|
|
|
|
style=${'white-space: nowrap' +
|
|
|
|
|
(this.show_version ? '' : '; display: none')}
|
2024-02-24 11:09:34 -05:00
|
|
|
|
title=${this.version?.name +
|
|
|
|
|
' ' +
|
|
|
|
|
Object.entries(this.version || {})
|
|
|
|
|
.filter((x) => ['name', 'number'].indexOf(x[0]) == -1)
|
|
|
|
|
.map((x) => `\n* ${x[0]}: ${x[1]}`)}
|
|
|
|
|
>${this.version?.number}</span
|
|
|
|
|
>
|
|
|
|
|
<a
|
2024-04-13 16:52:30 -04:00
|
|
|
|
class="w3-bar-item"
|
2024-02-24 11:09:34 -05:00
|
|
|
|
accesskey="h"
|
|
|
|
|
@mouseover=${set_access_key_title}
|
|
|
|
|
data-tip="Open home app."
|
|
|
|
|
href="/"
|
|
|
|
|
style="color: #fff; white-space: nowrap"
|
|
|
|
|
>TF</a
|
|
|
|
|
>
|
|
|
|
|
<a
|
2024-04-13 16:52:30 -04:00
|
|
|
|
class="w3-bar-item"
|
2024-02-24 11:09:34 -05:00
|
|
|
|
accesskey="a"
|
|
|
|
|
@mouseover=${set_access_key_title}
|
|
|
|
|
data-tip="Open apps list."
|
|
|
|
|
href="/~core/apps/"
|
|
|
|
|
>apps</a
|
|
|
|
|
>
|
|
|
|
|
<a
|
2024-04-13 16:52:30 -04:00
|
|
|
|
class="w3-bar-item"
|
2024-02-24 11:09:34 -05:00
|
|
|
|
accesskey="e"
|
|
|
|
|
@mouseover=${set_access_key_title}
|
|
|
|
|
data-tip="Toggle the app editor."
|
|
|
|
|
href="#"
|
|
|
|
|
@click=${this.toggle_edit}
|
|
|
|
|
>edit</a
|
|
|
|
|
>
|
|
|
|
|
<a
|
2024-04-13 16:52:30 -04:00
|
|
|
|
class="w3-bar-item"
|
2024-02-24 11:09:34 -05:00
|
|
|
|
accesskey="p"
|
|
|
|
|
@mouseover=${set_access_key_title}
|
|
|
|
|
data-tip="View and change permissions."
|
|
|
|
|
href="#"
|
|
|
|
|
@click=${() => (self.show_permissions = !self.show_permissions)}
|
|
|
|
|
>🎛️</a
|
|
|
|
|
>
|
2023-04-29 15:27:00 -04:00
|
|
|
|
${this.render_permissions()}
|
2024-04-18 12:46:06 -04:00
|
|
|
|
${this.status?.message && !this.status.is_error
|
|
|
|
|
? html`
|
|
|
|
|
<link type="text/css" rel="stylesheet" href="/static/w3.css" />
|
|
|
|
|
<div class="w3-bar-item" style="color: ${this.status.color ?? kStatusColor}">${this.status.message}</div>
|
|
|
|
|
`
|
|
|
|
|
: undefined}
|
2024-04-13 16:52:30 -04:00
|
|
|
|
${Object.keys(this.spark_lines)
|
|
|
|
|
.sort()
|
|
|
|
|
.map((x) => this.spark_lines[x])}
|
2024-04-13 20:07:39 -04:00
|
|
|
|
${this.render_login()} ${this.render_identity()}
|
2023-04-29 15:27:00 -04:00
|
|
|
|
</div>
|
2024-04-18 12:46:06 -04:00
|
|
|
|
${this.status?.is_error
|
|
|
|
|
? html`
|
|
|
|
|
<link type="text/css" rel="stylesheet" href="/static/w3.css" />
|
|
|
|
|
<div class="w3-model w3-animate-top" style="position: absolute; left: 50%; transform: translate(-50%); z-index: 1">
|
|
|
|
|
<dijv class="w3-modal-content w3-card-4" style="display: block; padding: 1em">
|
|
|
|
|
<span @click=${self.clear_error} class="w3-button w3-display-topright">×</span>
|
|
|
|
|
<div style="color: ${this.status.color ?? kErrorColor}"><b>ERROR:</b><p style="white-space: pre">${this.status.message}</p></div>
|
2024-04-17 20:56:33 -04:00
|
|
|
|
</div>
|
2024-04-18 12:46:06 -04:00
|
|
|
|
</div>
|
|
|
|
|
`
|
|
|
|
|
: undefined}
|
2023-04-29 14:23:08 -04:00
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-19 13:12:42 -05:00
|
|
|
|
|
2023-04-29 14:23:08 -04:00
|
|
|
|
customElements.define('tf-navigation', TfNavigationElement);
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
|
|
|
|
*/
|
2023-04-29 12:52:35 -04:00
|
|
|
|
class TfFilesElement extends LitElement {
|
|
|
|
|
static get properties() {
|
|
|
|
|
return {
|
|
|
|
|
current: {type: String},
|
|
|
|
|
files: {type: Object},
|
2023-10-17 17:37:42 -04:00
|
|
|
|
dropping: {type: Number},
|
2023-04-29 12:52:35 -04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
this.files = {};
|
2023-10-17 17:37:42 -04:00
|
|
|
|
this.dropping = 0;
|
2023-04-29 12:52:35 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} file
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2023-04-29 12:52:35 -04:00
|
|
|
|
file_click(file) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
this.dispatchEvent(
|
|
|
|
|
new CustomEvent('file_click', {
|
|
|
|
|
detail: {
|
|
|
|
|
file: file,
|
|
|
|
|
},
|
|
|
|
|
bubbles: true,
|
|
|
|
|
composed: true,
|
|
|
|
|
})
|
|
|
|
|
);
|
2023-04-29 12:52:35 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} file
|
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2023-04-29 12:52:35 -04:00
|
|
|
|
render_file(file) {
|
|
|
|
|
let classes = ['file'];
|
|
|
|
|
if (file == this.current) {
|
|
|
|
|
classes.push('current');
|
|
|
|
|
}
|
|
|
|
|
if (!this.files[file].clean) {
|
|
|
|
|
classes.push('dirty');
|
|
|
|
|
}
|
2024-02-24 11:09:34 -05:00
|
|
|
|
return html`<div
|
|
|
|
|
class="${classes.join(' ')}"
|
|
|
|
|
@click=${(x) => this.file_click(file)}
|
|
|
|
|
>
|
|
|
|
|
${file}
|
|
|
|
|
</div>`;
|
2023-04-29 12:52:35 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} event
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2023-10-17 17:37:42 -04:00
|
|
|
|
async drop(event) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
this.dropping = 0;
|
|
|
|
|
for (let file of event.dataTransfer.files) {
|
2023-10-18 13:51:26 -04:00
|
|
|
|
let buffer = await file.arrayBuffer();
|
|
|
|
|
let text = new TextDecoder('latin1').decode(buffer);
|
2023-10-17 17:37:42 -04:00
|
|
|
|
gFiles[file.name] = {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
doc: new cm6.EditorState.create({
|
|
|
|
|
doc: text,
|
|
|
|
|
extensions: cm6.extensions,
|
|
|
|
|
}),
|
2023-10-18 13:51:26 -04:00
|
|
|
|
buffer: buffer,
|
|
|
|
|
isNew: true,
|
2023-10-17 17:37:42 -04:00
|
|
|
|
};
|
|
|
|
|
gCurrentFile = file.name;
|
|
|
|
|
}
|
|
|
|
|
openFile(gCurrentFile);
|
|
|
|
|
updateFiles();
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} event
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2023-10-17 17:37:42 -04:00
|
|
|
|
drag_enter(event) {
|
|
|
|
|
this.dropping++;
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} event
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2023-10-17 17:37:42 -04:00
|
|
|
|
drag_leave(event) {
|
|
|
|
|
this.dropping--;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2023-04-29 12:52:35 -04:00
|
|
|
|
render() {
|
|
|
|
|
let self = this;
|
|
|
|
|
return html`
|
|
|
|
|
<style>
|
|
|
|
|
div.file {
|
|
|
|
|
padding: 0.5em;
|
2023-04-29 14:23:08 -04:00
|
|
|
|
cursor: pointer;
|
|
|
|
|
}
|
|
|
|
|
div.file:hover {
|
|
|
|
|
background-color: #1a9188;
|
2023-04-29 12:52:35 -04:00
|
|
|
|
}
|
|
|
|
|
div.file::before {
|
|
|
|
|
content: '📄 ';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
div.file.current {
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
background-color: #2aa198;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
div.file.dirty::after {
|
|
|
|
|
content: '*';
|
|
|
|
|
}
|
|
|
|
|
</style>
|
2024-02-24 11:09:34 -05:00
|
|
|
|
<div
|
|
|
|
|
@drop=${this.drop}
|
|
|
|
|
@dragenter=${this.drag_enter}
|
|
|
|
|
@dragleave=${this.drag_leave}
|
|
|
|
|
>
|
|
|
|
|
${Object.keys(this.files)
|
|
|
|
|
.sort()
|
|
|
|
|
.map((x) => self.render_file(x))}
|
2023-04-29 12:52:35 -04:00
|
|
|
|
</div>
|
2023-10-17 17:37:42 -04:00
|
|
|
|
<div
|
|
|
|
|
?hidden=${this.dropping == 0}
|
2024-02-24 11:09:34 -05:00
|
|
|
|
@drop=${this.drop}
|
|
|
|
|
@dragenter=${this.drag_enter}
|
|
|
|
|
@dragleave=${this.drag_leave}
|
|
|
|
|
style="text-align: center; vertical-align: middle; outline: 16px solid red; margin: -8px; background-color: rgba(255, 0, 0, 0.5); position: absolute; left: 16px; top: 16px; width: calc(100% - 16px); height: calc(100% - 16px); z-index: 1000"
|
|
|
|
|
>
|
2023-10-17 17:37:42 -04:00
|
|
|
|
Drop File(s)
|
|
|
|
|
</div>
|
2023-04-29 12:52:35 -04:00
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-19 13:12:42 -05:00
|
|
|
|
|
2023-04-29 12:52:35 -04:00
|
|
|
|
customElements.define('tf-files', TfFilesElement);
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
|
|
|
|
*/
|
2023-05-03 19:12:34 -04:00
|
|
|
|
class TfFilesPaneElement extends LitElement {
|
|
|
|
|
static get properties() {
|
|
|
|
|
return {
|
|
|
|
|
expanded: {type: Boolean},
|
|
|
|
|
current: {type: String},
|
|
|
|
|
files: {type: Object},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
2023-05-14 14:05:28 -04:00
|
|
|
|
this.expanded = window.localStorage.getItem('files') != '0';
|
2023-05-03 19:12:34 -04:00
|
|
|
|
this.files = {};
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} expanded
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2023-05-03 19:12:34 -04:00
|
|
|
|
set_expanded(expanded) {
|
|
|
|
|
this.expanded = expanded;
|
|
|
|
|
window.localStorage.setItem('files', expanded ? '1' : '0');
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2023-05-03 19:12:34 -04:00
|
|
|
|
render() {
|
|
|
|
|
let self = this;
|
2024-02-24 11:09:34 -05:00
|
|
|
|
let expander = this.expanded
|
|
|
|
|
? html`<div class="w3-button w3-bar-item w3-blue" style="flex: 0 0 auto; display: flex; flex-direction: row" @click=${() => self.set_expanded(false)}>
|
2024-01-20 11:05:00 -05:00
|
|
|
|
<span style="flex: 1 1" font-weight: bold; text-align: center; flex: 1">Files</span>
|
|
|
|
|
<span style="flex: 0 0">«</span>
|
2024-02-24 11:09:34 -05:00
|
|
|
|
</div>`
|
|
|
|
|
: html`<div
|
|
|
|
|
class="w3-button w3-bar-item w3-blue"
|
|
|
|
|
@click=${() => self.set_expanded(true)}
|
|
|
|
|
>
|
|
|
|
|
»
|
|
|
|
|
</div>`;
|
2023-05-03 19:12:34 -04:00
|
|
|
|
let content = html`
|
2024-02-24 11:09:34 -05:00
|
|
|
|
<tf-files
|
|
|
|
|
style="flex: 1 1; overflow: auto"
|
|
|
|
|
.files=${self.files}
|
|
|
|
|
current=${self.current}
|
|
|
|
|
@file_click=${(event) => openFile(event.detail.file)}
|
|
|
|
|
></tf-files>
|
|
|
|
|
<div>
|
|
|
|
|
<button
|
|
|
|
|
class="w3-bar-item w3-button w3-blue"
|
|
|
|
|
style="width: 100%; flex: 0 0"
|
|
|
|
|
@click=${() => newFile()}
|
|
|
|
|
accesskey="n"
|
|
|
|
|
@mouseover=${set_access_key_title}
|
|
|
|
|
data-tip="Add a new, empty file to the app"
|
|
|
|
|
>
|
|
|
|
|
📄 New File
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
<div>
|
|
|
|
|
<button
|
|
|
|
|
class="w3-bar-item w3-button w3-blue"
|
|
|
|
|
style="width: 100%; flex: 0 0"
|
|
|
|
|
@click=${() => removeFile()}
|
|
|
|
|
accesskey="r"
|
|
|
|
|
@mouseover=${set_access_key_title}
|
|
|
|
|
data-tip="Remove the selected file from the app"
|
|
|
|
|
>
|
|
|
|
|
🚮 Remove File
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
2023-05-03 19:12:34 -04:00
|
|
|
|
`;
|
|
|
|
|
return html`
|
2024-02-24 11:09:34 -05:00
|
|
|
|
<link type="text/css" rel="stylesheet" href="/static/w3.css" />
|
2024-01-20 11:05:00 -05:00
|
|
|
|
<div style="display: flex; flex-direction: column; height: 100%">
|
2024-02-24 11:09:34 -05:00
|
|
|
|
${expander} ${this.expanded ? content : undefined}
|
2023-05-03 19:12:34 -04:00
|
|
|
|
</div>
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-19 13:12:42 -05:00
|
|
|
|
|
2023-05-03 19:12:34 -04:00
|
|
|
|
customElements.define('tf-files-pane', TfFilesPaneElement);
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
|
|
|
|
*/
|
2023-04-29 15:27:00 -04:00
|
|
|
|
class TfSparkLineElement extends LitElement {
|
|
|
|
|
static get properties() {
|
|
|
|
|
return {
|
|
|
|
|
lines: {type: Array},
|
|
|
|
|
min: {type: Number},
|
|
|
|
|
max: {type: Number},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
this.min = 0;
|
|
|
|
|
this.max = 1.0;
|
|
|
|
|
this.lines = [];
|
2023-05-03 18:47:00 -04:00
|
|
|
|
this.k_values_max = 100;
|
2023-04-29 15:27:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} key
|
|
|
|
|
* @param {*} value
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2023-04-29 15:27:00 -04:00
|
|
|
|
append(key, value) {
|
|
|
|
|
let line = null;
|
|
|
|
|
for (let it of this.lines) {
|
|
|
|
|
if (it.name == key) {
|
|
|
|
|
line = it;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!line) {
|
|
|
|
|
const k_colors = ['#0f0', '#88f', '#ff0', '#f0f', '#0ff', '#f00', '#888'];
|
|
|
|
|
line = {
|
|
|
|
|
name: key,
|
|
|
|
|
style: k_colors[this.lines.length % k_colors.length],
|
2023-05-03 18:47:00 -04:00
|
|
|
|
values: Array(this.k_values_max).fill(0),
|
2023-04-29 15:27:00 -04:00
|
|
|
|
};
|
|
|
|
|
this.lines.push(line);
|
|
|
|
|
}
|
2023-05-03 18:47:00 -04:00
|
|
|
|
if (line.values.length >= this.k_values_max) {
|
2023-04-29 15:27:00 -04:00
|
|
|
|
line.values.shift();
|
|
|
|
|
}
|
2023-05-03 18:47:00 -04:00
|
|
|
|
line.values.push(value);
|
2023-04-29 15:27:00 -04:00
|
|
|
|
this.requestUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} line
|
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2023-04-29 15:27:00 -04:00
|
|
|
|
render_line(line) {
|
|
|
|
|
if (line?.values?.length >= 2) {
|
2023-05-03 18:47:00 -04:00
|
|
|
|
let max = Math.max(this.max, ...line.values);
|
2024-02-24 11:09:34 -05:00
|
|
|
|
let points = [].concat(
|
|
|
|
|
...line.values.map((x, i) => [
|
|
|
|
|
(50.0 * i) / (line.values.length - 1),
|
|
|
|
|
10.0 - (10.0 * (x - this.min)) / (max - this.min),
|
|
|
|
|
])
|
|
|
|
|
);
|
2023-05-03 18:47:00 -04:00
|
|
|
|
return svg`<polyline points=${points.join(' ')} stroke=${line.style} fill="none"/>`;
|
2023-04-29 15:27:00 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2023-04-29 15:27:00 -04:00
|
|
|
|
render() {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
let max =
|
|
|
|
|
Math.round(
|
|
|
|
|
10.0 *
|
|
|
|
|
Math.max(
|
|
|
|
|
...this.lines.map((line) => line.values[line.values.length - 1])
|
|
|
|
|
)
|
|
|
|
|
) / 10.0;
|
2023-04-29 15:27:00 -04:00
|
|
|
|
return html`
|
2024-02-24 11:09:34 -05:00
|
|
|
|
<svg
|
2024-04-13 16:52:30 -04:00
|
|
|
|
style="max-width: 7.5em; margin: 0; padding: 0; background: #000; height: 1em"
|
2024-02-24 11:09:34 -05:00
|
|
|
|
viewBox="0 0 50 10"
|
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
|
>
|
|
|
|
|
${this.lines.map((x) => this.render_line(x))}
|
|
|
|
|
<text x="0" y="1em" style="font: 8px sans-serif; fill: #fff">
|
2024-04-13 16:52:30 -04:00
|
|
|
|
${this.dataset.emoji}${max}
|
2024-02-24 11:09:34 -05:00
|
|
|
|
</text>
|
2023-04-29 15:27:00 -04:00
|
|
|
|
</svg>
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-19 13:12:42 -05:00
|
|
|
|
|
2023-04-29 15:27:00 -04:00
|
|
|
|
customElements.define('tf-sparkline', TfSparkLineElement);
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
// TODOC
|
2017-01-16 10:24:44 -05:00
|
|
|
|
window.addEventListener('keydown', function (event) {
|
2022-02-03 18:57:47 -05:00
|
|
|
|
if (event.keyCode == 83 && (event.altKey || event.ctrlKey)) {
|
2017-01-16 10:24:44 -05:00
|
|
|
|
if (editing()) {
|
|
|
|
|
save();
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
} else if (event.keyCode == 66 && event.altKey) {
|
|
|
|
|
if (editing()) {
|
|
|
|
|
closeEditor();
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} nodes
|
|
|
|
|
* @param {*} callback
|
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2017-01-16 10:24:44 -05:00
|
|
|
|
function ensureLoaded(nodes, callback) {
|
|
|
|
|
if (!nodes.length) {
|
|
|
|
|
callback();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-18 17:12:38 -04:00
|
|
|
|
let search = nodes.shift();
|
|
|
|
|
let head = document.head;
|
|
|
|
|
let found = false;
|
|
|
|
|
for (let i = 0; i < head.childNodes.length; i++) {
|
2017-01-16 10:24:44 -05:00
|
|
|
|
if (head.childNodes[i].tagName == search.tagName) {
|
2022-06-18 17:12:38 -04:00
|
|
|
|
let match = true;
|
|
|
|
|
for (let attribute in search.attributes) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
if (
|
|
|
|
|
head.childNodes[i].attributes[attribute].value !=
|
|
|
|
|
search.attributes[attribute]
|
|
|
|
|
) {
|
2017-01-16 10:24:44 -05:00
|
|
|
|
match = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (match) {
|
|
|
|
|
found = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (found) {
|
|
|
|
|
ensureLoaded(nodes, callback);
|
|
|
|
|
} else {
|
2022-06-18 17:12:38 -04:00
|
|
|
|
let node = document.createElement(search.tagName);
|
2024-02-24 11:09:34 -05:00
|
|
|
|
node.onreadystatechange = node.onload = function () {
|
2017-01-16 10:24:44 -05:00
|
|
|
|
ensureLoaded(nodes, callback);
|
|
|
|
|
};
|
2022-06-18 17:12:38 -04:00
|
|
|
|
for (let attribute in search.attributes) {
|
2017-01-16 10:24:44 -05:00
|
|
|
|
node.setAttribute(attribute, search.attributes[attribute]);
|
|
|
|
|
}
|
|
|
|
|
head.insertBefore(node, head.firstChild);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2017-01-16 10:24:44 -05:00
|
|
|
|
function editing() {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
return document.getElementById('editPane').style.display != 'none';
|
2017-01-16 10:24:44 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2023-08-16 20:49:02 -04:00
|
|
|
|
function is_edit_only() {
|
|
|
|
|
return window.location.search == '?editonly=1' || window.innerWidth < 1024;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2024-01-13 12:40:47 -05:00
|
|
|
|
async function edit() {
|
2017-01-16 10:24:44 -05:00
|
|
|
|
if (editing()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 16:06:20 -05:00
|
|
|
|
window.localStorage.setItem('editing', '1');
|
2024-02-24 11:09:34 -05:00
|
|
|
|
document.getElementById('editPane').style.display = 'flex';
|
|
|
|
|
document.getElementById('viewPane').style.display = is_edit_only()
|
|
|
|
|
? 'none'
|
|
|
|
|
: 'flex';
|
2022-01-12 21:18:40 -05:00
|
|
|
|
|
2024-01-13 12:40:47 -05:00
|
|
|
|
try {
|
2024-01-28 15:59:46 -05:00
|
|
|
|
if (!gEditor) {
|
|
|
|
|
cm6 = await import('/codemirror/cm6.js');
|
2024-02-24 11:09:34 -05:00
|
|
|
|
gEditor = cm6.TildeFriendsEditorView(document.getElementById('editor'));
|
2024-01-28 15:59:46 -05:00
|
|
|
|
}
|
2024-01-13 12:40:47 -05:00
|
|
|
|
gEditor.onDocChange = updateFiles;
|
|
|
|
|
await load();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
alert(`${error.message}\n\n${error.stack}`);
|
|
|
|
|
closeEditor();
|
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
|
|
|
|
*/
|
2022-01-02 14:10:45 -05:00
|
|
|
|
function trace() {
|
2023-02-17 19:51:22 -05:00
|
|
|
|
window.open(`/speedscope/#profileURL=${encodeURIComponent('/trace')}`);
|
2022-01-02 14:10:45 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} name
|
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
|
function guessMode(name) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
return name.endsWith('.js')
|
|
|
|
|
? 'javascript'
|
|
|
|
|
: name.endsWith('.html')
|
|
|
|
|
? 'htmlmixed'
|
|
|
|
|
: null;
|
2021-01-02 13:10:00 -05:00
|
|
|
|
}
|
2017-01-16 10:24:44 -05:00
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} name
|
|
|
|
|
* @param {*} id
|
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
|
function loadFile(name, id) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
return fetch('/' + id + '/view')
|
|
|
|
|
.then(function (response) {
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
alert(
|
|
|
|
|
`Request failed for ${name}: ${response.status} ${response.statusText}`
|
|
|
|
|
);
|
|
|
|
|
return 'missing file!';
|
|
|
|
|
}
|
|
|
|
|
return response.text();
|
|
|
|
|
})
|
|
|
|
|
.then(function (text) {
|
|
|
|
|
gFiles[name].doc = cm6.EditorState.create({
|
|
|
|
|
doc: text,
|
|
|
|
|
extensions: cm6.extensions,
|
|
|
|
|
});
|
|
|
|
|
gFiles[name].original = gFiles[name].doc.doc.toString();
|
|
|
|
|
if (!Object.values(gFiles).some((x) => !x.doc)) {
|
|
|
|
|
openFile(Object.keys(gFiles).sort()[0]);
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-01-16 10:24:44 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} path
|
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2024-01-13 12:40:47 -05:00
|
|
|
|
async function load(path) {
|
|
|
|
|
let response = await fetch((path || url()) + 'view');
|
2024-01-28 08:09:31 -05:00
|
|
|
|
let json;
|
|
|
|
|
if (response.ok) {
|
|
|
|
|
json = await response.json();
|
|
|
|
|
} else if (response.status != 404) {
|
|
|
|
|
throw new Error(response.status + ' ' + response.statusText);
|
2024-01-13 12:40:47 -05:00
|
|
|
|
}
|
|
|
|
|
gFiles = {};
|
|
|
|
|
let isApp = false;
|
|
|
|
|
let promises = [];
|
|
|
|
|
|
|
|
|
|
if (json && json['type'] == 'tildefriends-app') {
|
|
|
|
|
isApp = true;
|
2024-02-24 11:09:34 -05:00
|
|
|
|
Object.keys(json['files']).forEach(function (name) {
|
2024-01-13 12:40:47 -05:00
|
|
|
|
gFiles[name] = {};
|
|
|
|
|
promises.push(loadFile(name, json['files'][name]));
|
|
|
|
|
});
|
|
|
|
|
if (Object.keys(json['files']).length == 0) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
document.getElementById('editPane').style.display = 'flex';
|
2022-02-16 21:29:04 -05:00
|
|
|
|
}
|
2024-01-13 12:40:47 -05:00
|
|
|
|
gApp = json;
|
|
|
|
|
gApp.emoji = gApp.emoji || '📦';
|
|
|
|
|
document.getElementById('icon').innerHTML = gApp.emoji;
|
|
|
|
|
}
|
|
|
|
|
if (!isApp) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
document.getElementById('editPane').style.display = 'flex';
|
2024-01-13 12:40:47 -05:00
|
|
|
|
let text = '// New script.\n';
|
|
|
|
|
gCurrentFile = 'app.js';
|
|
|
|
|
gFiles[gCurrentFile] = {
|
|
|
|
|
doc: cm6.EditorState.create({doc: text, extensions: cm6.extensions}),
|
|
|
|
|
};
|
|
|
|
|
openFile(gCurrentFile);
|
|
|
|
|
}
|
|
|
|
|
return Promise.all(promises);
|
2017-01-16 10:24:44 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
|
|
|
|
*/
|
2017-01-16 10:24:44 -05:00
|
|
|
|
function closeEditor() {
|
2022-03-07 16:06:20 -05:00
|
|
|
|
window.localStorage.setItem('editing', '0');
|
2024-02-24 11:09:34 -05:00
|
|
|
|
document.getElementById('editPane').style.display = 'none';
|
2023-08-16 20:49:02 -04:00
|
|
|
|
document.getElementById('viewPane').style.display = 'flex';
|
2017-01-16 10:24:44 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2017-01-16 10:24:44 -05:00
|
|
|
|
function explodePath() {
|
|
|
|
|
return /^\/~([^\/]+)\/([^\/]+)(.*)/.exec(window.location.pathname);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} save_to
|
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2022-01-30 09:51:09 -05:00
|
|
|
|
function save(save_to) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
document.getElementById('save').disabled = true;
|
2021-01-02 13:10:00 -05:00
|
|
|
|
if (gCurrentFile) {
|
2024-01-13 12:40:47 -05:00
|
|
|
|
gFiles[gCurrentFile].doc = gEditor.state;
|
2024-02-24 11:09:34 -05:00
|
|
|
|
if (
|
|
|
|
|
!gFiles[gCurrentFile].isNew &&
|
|
|
|
|
!gFiles[gCurrentFile].doc.doc.toString() == gFiles[gCurrentFile].original
|
|
|
|
|
) {
|
2023-10-18 13:51:26 -04:00
|
|
|
|
delete gFiles[gCurrentFile].buffer;
|
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
|
}
|
2017-01-16 10:24:44 -05:00
|
|
|
|
|
2022-06-18 17:12:38 -04:00
|
|
|
|
let save_path = save_to;
|
2022-02-16 21:29:04 -05:00
|
|
|
|
if (!save_path) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
let name = document.getElementById('name');
|
2022-01-30 09:51:09 -05:00
|
|
|
|
if (name && name.value) {
|
|
|
|
|
save_path = name.value;
|
|
|
|
|
} else {
|
|
|
|
|
save_path = url();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-18 17:12:38 -04:00
|
|
|
|
let promises = [];
|
2022-02-16 21:29:04 -05:00
|
|
|
|
for (let name of Object.keys(gFiles)) {
|
|
|
|
|
let file = gFiles[name];
|
2024-01-13 12:40:47 -05:00
|
|
|
|
if (!file.isNew && file.doc.doc.toString() == file.original) {
|
2022-02-16 21:29:04 -05:00
|
|
|
|
continue;
|
2021-01-12 21:40:46 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
|
delete file.id;
|
2023-10-18 13:51:26 -04:00
|
|
|
|
delete file.isNew;
|
2024-02-24 11:09:34 -05:00
|
|
|
|
promises.push(
|
|
|
|
|
fetch('/save', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/binary',
|
|
|
|
|
},
|
|
|
|
|
body: file.buffer ?? file.doc.doc.toString(),
|
|
|
|
|
})
|
|
|
|
|
.then(function (response) {
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
'Saving "' +
|
|
|
|
|
name +
|
|
|
|
|
'": ' +
|
|
|
|
|
response.status +
|
|
|
|
|
' ' +
|
|
|
|
|
response.statusText
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return response.text();
|
|
|
|
|
})
|
|
|
|
|
.then(function (text) {
|
|
|
|
|
file.id = text;
|
|
|
|
|
if (file.id.charAt(0) == '/') {
|
|
|
|
|
file.id = file.id.substr(1);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
);
|
2022-02-16 21:29:04 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-24 11:09:34 -05:00
|
|
|
|
return Promise.all(promises)
|
|
|
|
|
.then(function () {
|
|
|
|
|
let app = {
|
|
|
|
|
type: 'tildefriends-app',
|
|
|
|
|
files: Object.fromEntries(
|
|
|
|
|
Object.keys(gFiles).map((x) => [x, gFiles[x].id || gApp.files[x]])
|
|
|
|
|
),
|
|
|
|
|
emoji: gApp.emoji || '📦',
|
|
|
|
|
};
|
|
|
|
|
Object.values(gFiles).forEach(function (file) {
|
|
|
|
|
delete file.id;
|
|
|
|
|
});
|
|
|
|
|
gApp = JSON.parse(JSON.stringify(app));
|
|
|
|
|
|
|
|
|
|
return fetch(save_path + 'save', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify(app),
|
|
|
|
|
}).then(function (response) {
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(response.status + ' ' + response.statusText);
|
|
|
|
|
}
|
2024-02-23 04:35:39 -05:00
|
|
|
|
|
2024-02-24 11:09:34 -05:00
|
|
|
|
if (save_path != window.location.pathname) {
|
|
|
|
|
alert('Saved to ' + save_path + '.');
|
|
|
|
|
} else {
|
|
|
|
|
reconnect(save_path);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.catch(function (error) {
|
|
|
|
|
alert(error);
|
|
|
|
|
})
|
|
|
|
|
.finally(function () {
|
|
|
|
|
document.getElementById('save').disabled = false;
|
|
|
|
|
Object.values(gFiles).forEach(function (file) {
|
|
|
|
|
file.original = file.doc.doc.toString();
|
|
|
|
|
});
|
|
|
|
|
updateFiles();
|
2021-01-02 13:10:00 -05:00
|
|
|
|
});
|
2016-03-12 13:50:43 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
|
|
|
|
*/
|
2023-03-21 19:08:04 -04:00
|
|
|
|
function changeIcon() {
|
|
|
|
|
let value = prompt('Enter a new app icon emoji:');
|
|
|
|
|
if (value !== undefined) {
|
|
|
|
|
gApp.emoji = value || '📦';
|
2024-01-06 10:47:14 -05:00
|
|
|
|
document.getElementById('icon').innerHTML = gApp.emoji;
|
2023-03-21 19:08:04 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
|
|
|
|
*/
|
2022-06-20 14:13:19 -04:00
|
|
|
|
function deleteApp() {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
let name = document.getElementById('name');
|
2022-06-20 14:13:19 -04:00
|
|
|
|
let path = name && name.value ? name.value : url();
|
|
|
|
|
|
|
|
|
|
if (confirm(`Are you sure you want to delete the app '${path}'?`)) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
fetch(path + 'delete')
|
|
|
|
|
.then(function (response) {
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(response.status + ' ' + response.statusText);
|
|
|
|
|
}
|
|
|
|
|
alert('Deleted.');
|
|
|
|
|
})
|
|
|
|
|
.catch(function (error) {
|
|
|
|
|
alert(error);
|
|
|
|
|
});
|
2022-06-20 14:13:19 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2016-03-12 13:50:43 -05:00
|
|
|
|
function url() {
|
2022-06-18 17:12:38 -04:00
|
|
|
|
let hash = window.location.href.indexOf('#');
|
|
|
|
|
let question = window.location.href.indexOf('?');
|
|
|
|
|
let end = -1;
|
2024-02-24 11:09:34 -05:00
|
|
|
|
if (hash != -1 && (hash < end || end == -1)) {
|
2016-04-06 21:30:07 -04:00
|
|
|
|
end = hash;
|
|
|
|
|
}
|
2024-02-24 11:09:34 -05:00
|
|
|
|
if (question != -1 && (question < end || end == -1)) {
|
2016-04-06 21:30:07 -04:00
|
|
|
|
end = question;
|
|
|
|
|
}
|
2024-02-24 11:09:34 -05:00
|
|
|
|
return end != -1
|
|
|
|
|
? window.location.href.substring(0, end)
|
|
|
|
|
: window.location.href;
|
2016-03-12 13:50:43 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2016-04-06 21:30:07 -04:00
|
|
|
|
function hash() {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
return window.location.hash != '#' ? window.location.hash : '';
|
2016-04-06 21:30:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} content
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2022-06-18 17:12:38 -04:00
|
|
|
|
function api_setDocument(content) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
let iframe = document.getElementById('document');
|
2022-06-18 17:12:38 -04:00
|
|
|
|
iframe.srcdoc = content;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} message
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2022-06-18 17:12:38 -04:00
|
|
|
|
function api_postMessage(message) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
let iframe = document.getElementById('document');
|
|
|
|
|
iframe.contentWindow.postMessage(message, '*');
|
2022-06-18 17:12:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} error
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2022-06-18 17:12:38 -04:00
|
|
|
|
function api_error(error) {
|
|
|
|
|
if (error) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
if (typeof error == 'string') {
|
2024-04-18 12:46:06 -04:00
|
|
|
|
setStatusMessage('⚠️ ' + error, kErrorColor);
|
2022-06-18 17:12:38 -04:00
|
|
|
|
} else {
|
2024-04-18 12:46:06 -04:00
|
|
|
|
setStatusMessage('⚠️ ' + error.message + '\n' + error.stack, kErrorColor);
|
2022-06-18 17:12:38 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
console.log('error', error);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} key
|
|
|
|
|
* @param {*} value
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2022-06-18 17:12:38 -04:00
|
|
|
|
function api_localStorageSet(key, value) {
|
|
|
|
|
window.localStorage.setItem('app:' + key, value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} key
|
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2023-01-20 19:16:18 -05:00
|
|
|
|
function api_localStorageGet(key) {
|
2022-08-13 14:58:06 -04:00
|
|
|
|
return window.localStorage.getItem('app:' + key);
|
2022-06-18 17:12:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} permission
|
|
|
|
|
* @param {*} id
|
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2022-07-26 20:27:10 -04:00
|
|
|
|
function api_requestPermission(permission, id) {
|
2023-04-29 20:56:59 -04:00
|
|
|
|
let outer = document.createElement('div');
|
|
|
|
|
outer.classList.add('permissions');
|
2022-08-07 18:39:58 -04:00
|
|
|
|
|
|
|
|
|
let container = document.createElement('div');
|
|
|
|
|
container.classList.add('permissions_contents');
|
|
|
|
|
|
2022-07-26 20:27:10 -04:00
|
|
|
|
let div = document.createElement('div');
|
2024-02-24 11:09:34 -05:00
|
|
|
|
div.appendChild(
|
|
|
|
|
document.createTextNode('This app is requesting the following permission:')
|
|
|
|
|
);
|
2022-08-07 18:39:58 -04:00
|
|
|
|
let span = document.createElement('span');
|
|
|
|
|
span.style = 'font-weight: bold';
|
|
|
|
|
span.appendChild(document.createTextNode(permission));
|
|
|
|
|
div.appendChild(span);
|
|
|
|
|
container.appendChild(div);
|
|
|
|
|
|
|
|
|
|
div = document.createElement('div');
|
|
|
|
|
div.style = 'padding: 1em';
|
|
|
|
|
let check = document.createElement('input');
|
|
|
|
|
check.id = 'permissions_remember_check';
|
|
|
|
|
check.type = 'checkbox';
|
2024-01-18 21:32:55 -05:00
|
|
|
|
check.classList.add('w3-check');
|
|
|
|
|
check.classList.add('w3-blue');
|
2022-08-07 18:39:58 -04:00
|
|
|
|
div.appendChild(check);
|
|
|
|
|
let label = document.createElement('label');
|
|
|
|
|
label.htmlFor = check.id;
|
|
|
|
|
label.appendChild(document.createTextNode('Remember this decision.'));
|
|
|
|
|
div.appendChild(label);
|
|
|
|
|
container.appendChild(div);
|
|
|
|
|
|
|
|
|
|
const k_options = [
|
|
|
|
|
{
|
2023-08-02 20:30:48 -04:00
|
|
|
|
id: 'allow',
|
2022-08-07 18:39:58 -04:00
|
|
|
|
text: '✅ Allow',
|
|
|
|
|
grant: ['allow once', 'allow'],
|
|
|
|
|
},
|
|
|
|
|
{
|
2023-08-02 20:30:48 -04:00
|
|
|
|
id: 'deny',
|
2022-08-07 18:39:58 -04:00
|
|
|
|
text: '❌ Deny',
|
|
|
|
|
grant: ['deny once', 'deny'],
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2024-02-24 11:09:34 -05:00
|
|
|
|
return new Promise(function (resolve, reject) {
|
2022-08-13 21:46:11 -04:00
|
|
|
|
div = document.createElement('div');
|
|
|
|
|
for (let option of k_options) {
|
|
|
|
|
let button = document.createElement('button');
|
2024-01-18 21:32:55 -05:00
|
|
|
|
button.classList.add('w3-button');
|
|
|
|
|
button.classList.add('w3-blue');
|
2022-08-13 21:46:11 -04:00
|
|
|
|
button.innerText = option.text;
|
2023-08-02 20:30:48 -04:00
|
|
|
|
button.id = option.id;
|
2024-02-24 11:09:34 -05:00
|
|
|
|
button.onclick = function () {
|
2022-08-13 21:46:11 -04:00
|
|
|
|
resolve(option.grant[check.checked ? 1 : 0]);
|
2023-04-29 20:56:59 -04:00
|
|
|
|
document.body.removeChild(outer);
|
2024-02-24 11:09:34 -05:00
|
|
|
|
};
|
2022-08-13 21:46:11 -04:00
|
|
|
|
div.appendChild(button);
|
2022-07-26 20:27:10 -04:00
|
|
|
|
}
|
2022-08-13 21:46:11 -04:00
|
|
|
|
container.appendChild(div);
|
2023-04-29 20:56:59 -04:00
|
|
|
|
outer.appendChild(container);
|
2022-08-07 18:39:58 -04:00
|
|
|
|
|
2023-04-29 20:56:59 -04:00
|
|
|
|
document.body.appendChild(outer);
|
2022-08-13 21:46:11 -04:00
|
|
|
|
});
|
2022-07-26 20:27:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
|
|
|
|
*/
|
2022-08-14 12:58:26 -04:00
|
|
|
|
function api_print() {
|
|
|
|
|
console.log('app>', ...arguments);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} hash
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2022-09-14 20:16:37 -04:00
|
|
|
|
function api_setHash(hash) {
|
|
|
|
|
window.location.hash = hash;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} message
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2022-08-14 12:58:26 -04:00
|
|
|
|
function _receive_websocket_message(message) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
if (message && message.action == 'session') {
|
|
|
|
|
setStatusMessage('🟢 Executing...', kStatusColor);
|
2024-04-13 13:22:59 -04:00
|
|
|
|
let navigation = document.getElementsByTagName('tf-navigation')[0];
|
|
|
|
|
navigation.credentials = message.credentials;
|
|
|
|
|
navigation.identities = message.identities;
|
|
|
|
|
navigation.identity = message.identity;
|
2024-04-13 21:51:18 -04:00
|
|
|
|
navigation.names = message.names;
|
2022-08-14 14:24:41 -04:00
|
|
|
|
} else if (message && message.action == 'permissions') {
|
2024-04-13 13:22:59 -04:00
|
|
|
|
let navigation = document.getElementsByTagName('tf-navigation')[0];
|
|
|
|
|
navigation.permissions = message.permissions ?? {};
|
|
|
|
|
} else if (message && message.action == 'identities') {
|
|
|
|
|
let navigation = document.getElementsByTagName('tf-navigation')[0];
|
|
|
|
|
navigation.identities = message.identities;
|
|
|
|
|
navigation.identity = message.identity;
|
2024-04-13 21:51:18 -04:00
|
|
|
|
navigation.names = message.names;
|
2024-02-24 11:09:34 -05:00
|
|
|
|
} else if (message && message.action == 'ready') {
|
2021-01-02 13:10:00 -05:00
|
|
|
|
setStatusMessage(null);
|
|
|
|
|
if (window.location.hash) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
send({event: 'hashChange', hash: window.location.hash});
|
2021-01-02 13:10:00 -05:00
|
|
|
|
}
|
2023-06-28 19:00:34 -04:00
|
|
|
|
document.getElementsByTagName('tf-navigation')[0].version = message.version;
|
2024-02-24 11:09:34 -05:00
|
|
|
|
document.getElementById('viewPane').style.display = message.edit_only
|
|
|
|
|
? 'none'
|
|
|
|
|
: 'flex';
|
2023-04-29 15:27:00 -04:00
|
|
|
|
send({action: 'enableStats', enabled: true});
|
2024-02-24 11:09:34 -05:00
|
|
|
|
} else if (message && message.action == 'ping') {
|
|
|
|
|
send({action: 'pong'});
|
|
|
|
|
} else if (message && message.action == 'stats') {
|
2022-06-18 17:12:38 -04:00
|
|
|
|
let now = new Date().getTime();
|
|
|
|
|
for (let key of Object.keys(message.stats)) {
|
2022-06-04 12:38:45 -04:00
|
|
|
|
const k_groups = {
|
|
|
|
|
rpc_in: {group: 'rpc', name: 'in'},
|
|
|
|
|
rpc_out: {group: 'rpc', name: 'out'},
|
|
|
|
|
|
2023-02-28 20:36:26 -05:00
|
|
|
|
cpu_percent: {group: 'cpu', name: 'main'},
|
|
|
|
|
thread_percent: {group: 'cpu', name: 'work'},
|
|
|
|
|
|
2022-06-17 17:18:10 -04:00
|
|
|
|
arena_percent: {group: 'memory', name: 'm'},
|
2022-06-04 12:38:45 -04:00
|
|
|
|
js_malloc_percent: {group: 'memory', name: 'js'},
|
2022-06-17 17:18:10 -04:00
|
|
|
|
memory_percent: {group: 'memory', name: 'tot'},
|
2022-06-04 12:38:45 -04:00
|
|
|
|
sqlite3_memory_percent: {group: 'memory', name: 'sql'},
|
2022-06-04 13:04:51 -04:00
|
|
|
|
tf_malloc_percent: {group: 'memory', name: 'tf'},
|
2022-06-04 12:38:45 -04:00
|
|
|
|
tls_malloc_percent: {group: 'memory', name: 'tls'},
|
|
|
|
|
uv_malloc_percent: {group: 'memory', name: 'uv'},
|
|
|
|
|
|
2023-04-29 15:46:33 -04:00
|
|
|
|
messages_stored: {group: 'store', name: 'messages'},
|
|
|
|
|
blobs_stored: {group: 'store', name: 'blobs'},
|
2023-01-18 17:52:54 -05:00
|
|
|
|
|
2022-06-04 12:38:45 -04:00
|
|
|
|
socket_count: {group: 'socket', name: 'total'},
|
|
|
|
|
socket_open_count: {group: 'socket', name: 'open'},
|
|
|
|
|
|
|
|
|
|
import_count: {group: 'functions', name: 'imports'},
|
|
|
|
|
export_count: {group: 'functions', name: 'exports'},
|
|
|
|
|
};
|
2022-06-17 17:18:10 -04:00
|
|
|
|
const k_colors = ['#0f0', '#88f', '#ff0', '#f0f', '#0ff', '#f00', '#888'];
|
2022-06-04 12:38:45 -04:00
|
|
|
|
let graph_key = k_groups[key]?.group || key;
|
2023-09-04 16:13:17 -04:00
|
|
|
|
if (['cpu', 'rpc', 'store', 'memory'].indexOf(graph_key) != -1) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
let line = document
|
|
|
|
|
.getElementsByTagName('tf-navigation')[0]
|
|
|
|
|
.get_spark_line(graph_key, {max: 100});
|
2023-04-29 15:46:33 -04:00
|
|
|
|
line.dataset.emoji = {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
cpu: '💻',
|
|
|
|
|
rpc: '🔁',
|
|
|
|
|
store: '💾',
|
|
|
|
|
memory: '🐏',
|
2023-04-29 15:46:33 -04:00
|
|
|
|
}[graph_key];
|
|
|
|
|
line.append(key, message.stats[key]);
|
2023-04-29 15:27:00 -04:00
|
|
|
|
}
|
2022-01-17 16:46:32 -05:00
|
|
|
|
}
|
2024-02-24 11:09:34 -05:00
|
|
|
|
} else if (message && message.message === 'tfrpc' && message.method) {
|
2022-08-13 14:58:06 -04:00
|
|
|
|
let api = k_api[message.method];
|
2023-01-20 19:16:18 -05:00
|
|
|
|
let id = message.id;
|
|
|
|
|
let params = message.params;
|
2022-06-18 17:12:38 -04:00
|
|
|
|
if (api) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
Promise.resolve(api.func(...params))
|
|
|
|
|
.then(function (result) {
|
|
|
|
|
send({
|
|
|
|
|
message: 'tfrpc',
|
|
|
|
|
id: id,
|
|
|
|
|
result: result,
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.catch(function (error) {
|
|
|
|
|
send({
|
|
|
|
|
message: 'tfrpc',
|
|
|
|
|
id: id,
|
|
|
|
|
error: error,
|
|
|
|
|
});
|
2022-08-13 14:58:06 -04:00
|
|
|
|
});
|
2022-06-18 17:12:38 -04:00
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} message
|
|
|
|
|
* @param {*} color
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2022-06-18 16:51:22 -04:00
|
|
|
|
function setStatusMessage(message, color) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
document.getElementsByTagName('tf-navigation')[0].status = {
|
|
|
|
|
message: message,
|
|
|
|
|
color: color,
|
2024-04-18 12:46:06 -04:00
|
|
|
|
is_error: color == kErrorColor,
|
2024-02-24 11:09:34 -05:00
|
|
|
|
};
|
2016-04-10 20:28:42 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} value
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
|
function send(value) {
|
2016-04-10 20:09:21 -04:00
|
|
|
|
try {
|
2022-06-18 16:51:22 -04:00
|
|
|
|
if (gSocket && gSocket.readyState == gSocket.OPEN) {
|
|
|
|
|
gSocket.send(JSON.stringify(value));
|
|
|
|
|
}
|
2016-04-10 20:09:21 -04:00
|
|
|
|
} catch (error) {
|
2022-06-18 16:51:22 -04:00
|
|
|
|
setStatusMessage('🤷 Send failed: ' + error.toString(), kErrorColor);
|
2016-04-10 20:09:21 -04:00
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} sourceData
|
|
|
|
|
* @param {*} maxWidth
|
|
|
|
|
* @param {*} maxHeight
|
|
|
|
|
* @param {*} callback
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2016-03-12 13:50:43 -05:00
|
|
|
|
function fixImage(sourceData, maxWidth, maxHeight, callback) {
|
2022-06-18 17:12:38 -04:00
|
|
|
|
let result = sourceData;
|
|
|
|
|
let image = new Image();
|
2024-02-24 11:09:34 -05:00
|
|
|
|
image.crossOrigin = 'anonymous';
|
|
|
|
|
image.referrerPolicy = 'no-referrer';
|
|
|
|
|
image.onload = function () {
|
2016-03-12 13:50:43 -05:00
|
|
|
|
if (image.width > maxWidth || image.height > maxHeight) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
let downScale = Math.min(
|
|
|
|
|
maxWidth / image.width,
|
|
|
|
|
maxHeight / image.height
|
|
|
|
|
);
|
|
|
|
|
let canvas = document.createElement('canvas');
|
2016-03-12 13:50:43 -05:00
|
|
|
|
canvas.width = image.width * downScale;
|
|
|
|
|
canvas.height = image.height * downScale;
|
2024-02-24 11:09:34 -05:00
|
|
|
|
let context = canvas.getContext('2d');
|
2016-03-12 13:50:43 -05:00
|
|
|
|
context.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
|
image.width = canvas.width;
|
|
|
|
|
image.height = canvas.height;
|
|
|
|
|
context.drawImage(image, 0, 0, image.width, image.height);
|
|
|
|
|
result = canvas.toDataURL();
|
|
|
|
|
}
|
|
|
|
|
callback(result);
|
|
|
|
|
};
|
|
|
|
|
image.src = sourceData;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} image
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2016-03-12 13:50:43 -05:00
|
|
|
|
function sendImage(image) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
fixImage(image, 320, 240, function (result) {
|
2016-03-12 13:50:43 -05:00
|
|
|
|
send({image: result});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
|
|
|
|
*/
|
2016-03-12 13:50:43 -05:00
|
|
|
|
function hashChange() {
|
|
|
|
|
send({event: 'hashChange', hash: window.location.hash});
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
|
|
|
|
*/
|
2016-03-12 13:50:43 -05:00
|
|
|
|
function focus() {
|
2016-05-07 07:07:54 -04:00
|
|
|
|
if (gSocket && gSocket.readyState == gSocket.CLOSED) {
|
|
|
|
|
connectSocket();
|
|
|
|
|
} else {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
send({event: 'focus'});
|
2016-05-07 07:07:54 -04:00
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
|
|
|
|
*/
|
2016-03-12 13:50:43 -05:00
|
|
|
|
function blur() {
|
2016-05-07 07:07:54 -04:00
|
|
|
|
if (gSocket && gSocket.readyState == gSocket.OPEN) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
send({event: 'blur'});
|
2016-05-07 07:07:54 -04:00
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} event
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
|
function message(event) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
if (
|
|
|
|
|
event.data &&
|
|
|
|
|
event.data.event == 'resizeMe' &&
|
|
|
|
|
event.data.width &&
|
|
|
|
|
event.data.height
|
|
|
|
|
) {
|
|
|
|
|
let iframe = document.getElementById('iframe_' + event.data.name);
|
|
|
|
|
iframe.setAttribute('width', event.data.width);
|
|
|
|
|
iframe.setAttribute('height', event.data.height);
|
|
|
|
|
} else if (event.data && event.data.action == 'setHash') {
|
2022-01-06 20:52:47 -05:00
|
|
|
|
window.location.hash = event.data.hash;
|
2022-01-27 22:11:09 -05:00
|
|
|
|
} else if (event.data && event.data.action == 'storeBlob') {
|
2022-02-16 21:29:04 -05:00
|
|
|
|
fetch('/save', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/binary',
|
|
|
|
|
},
|
|
|
|
|
body: event.data.blob.buffer,
|
2024-02-24 11:09:34 -05:00
|
|
|
|
})
|
|
|
|
|
.then(function (response) {
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(response.status + ' ' + response.statusText);
|
|
|
|
|
}
|
|
|
|
|
return response.text();
|
|
|
|
|
})
|
|
|
|
|
.then(function (text) {
|
|
|
|
|
let iframe = document.getElementById('document');
|
|
|
|
|
iframe.contentWindow.postMessage(
|
|
|
|
|
{
|
|
|
|
|
storeBlobComplete: {
|
|
|
|
|
name: event.data.blob.name,
|
|
|
|
|
path: text,
|
|
|
|
|
type: event.data.blob.type,
|
|
|
|
|
context: event.data.context,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
'*'
|
|
|
|
|
);
|
|
|
|
|
});
|
2016-09-17 16:53:03 -04:00
|
|
|
|
} else {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
send({event: 'message', message: event.data});
|
2016-05-01 09:24:37 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-10 20:09:21 -04:00
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} path
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
|
function reconnect(path) {
|
2017-01-16 10:24:44 -05:00
|
|
|
|
let oldSocket = gSocket;
|
2024-02-24 11:09:34 -05:00
|
|
|
|
gSocket = null;
|
2023-03-12 18:16:18 -04:00
|
|
|
|
if (oldSocket) {
|
|
|
|
|
oldSocket.onopen = null;
|
|
|
|
|
oldSocket.onclose = null;
|
|
|
|
|
oldSocket.onmessage = null;
|
|
|
|
|
oldSocket.close();
|
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
|
connectSocket(path);
|
2017-01-16 10:24:44 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} path
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
|
function connectSocket(path) {
|
2021-01-19 21:01:14 -05:00
|
|
|
|
if (!gSocket || gSocket.readyState != gSocket.OPEN) {
|
|
|
|
|
if (gSocket) {
|
|
|
|
|
gSocket.onopen = null;
|
|
|
|
|
gSocket.onclose = null;
|
|
|
|
|
gSocket.onmessage = null;
|
|
|
|
|
gSocket.close();
|
|
|
|
|
}
|
2024-02-24 11:09:34 -05:00
|
|
|
|
setStatusMessage('⚪ Connecting...', kStatusColor);
|
2016-05-07 07:07:54 -04:00
|
|
|
|
gSocket = new WebSocket(
|
2024-02-24 11:09:34 -05:00
|
|
|
|
(window.location.protocol == 'https:' ? 'wss://' : 'ws://') +
|
|
|
|
|
window.location.hostname +
|
|
|
|
|
(window.location.port.length ? ':' + window.location.port : '') +
|
|
|
|
|
'/app/socket'
|
|
|
|
|
);
|
|
|
|
|
gSocket.onopen = function () {
|
|
|
|
|
setStatusMessage('🟡 Authenticating...', kStatusColor);
|
2022-08-07 21:48:23 -04:00
|
|
|
|
let connect_path = path ?? window.location.pathname;
|
2024-02-24 11:09:34 -05:00
|
|
|
|
gSocket.send(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
action: 'hello',
|
|
|
|
|
path: connect_path,
|
|
|
|
|
url: window.location.href,
|
|
|
|
|
edit_only: editing() && is_edit_only(),
|
|
|
|
|
api: Object.entries(k_api).map(([key, value]) =>
|
|
|
|
|
[].concat([key], value.args)
|
|
|
|
|
),
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
gSocket.onmessage = function (event) {
|
2022-08-14 12:58:26 -04:00
|
|
|
|
_receive_websocket_message(JSON.parse(event.data));
|
2024-02-24 11:09:34 -05:00
|
|
|
|
};
|
|
|
|
|
gSocket.onclose = function (event) {
|
2022-01-29 15:43:19 -05:00
|
|
|
|
const k_codes = {
|
|
|
|
|
1000: 'Normal closure',
|
|
|
|
|
1001: 'Going away',
|
|
|
|
|
1002: 'Protocol error',
|
|
|
|
|
1003: 'Unsupported data',
|
|
|
|
|
1005: 'No status received',
|
|
|
|
|
1006: 'Abnormal closure',
|
|
|
|
|
1007: 'Invalid frame payload data',
|
|
|
|
|
1008: 'Policy violation',
|
|
|
|
|
1009: 'Message too big',
|
|
|
|
|
1010: 'Missing extension',
|
|
|
|
|
1011: 'Internal error',
|
|
|
|
|
1012: 'Service restart',
|
|
|
|
|
1013: 'Try again later',
|
|
|
|
|
1014: 'Bad gateway',
|
|
|
|
|
1015: 'TLS handshake',
|
|
|
|
|
};
|
2024-02-24 11:09:34 -05:00
|
|
|
|
setStatusMessage(
|
|
|
|
|
'🔴 Closed: ' + (k_codes[event.code] || event.code),
|
|
|
|
|
kErrorColor
|
|
|
|
|
);
|
|
|
|
|
};
|
2016-05-07 07:07:54 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} name
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
|
function openFile(name) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
let newDoc =
|
|
|
|
|
name && gFiles[name]
|
|
|
|
|
? gFiles[name].doc
|
|
|
|
|
: cm6.EditorState.create({doc: '', extensions: cm6.extensions});
|
2024-01-13 12:40:47 -05:00
|
|
|
|
let oldDoc = gEditor.state;
|
|
|
|
|
gEditor.setState(newDoc);
|
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
|
if (gFiles[gCurrentFile]) {
|
|
|
|
|
gFiles[gCurrentFile].doc = oldDoc;
|
2024-02-24 11:09:34 -05:00
|
|
|
|
if (
|
|
|
|
|
!gFiles[gCurrentFile].isNew &&
|
|
|
|
|
gFiles[gCurrentFile].doc.doc.toString() == oldDoc.doc.toString()
|
|
|
|
|
) {
|
2023-10-18 13:51:26 -04:00
|
|
|
|
delete gFiles[gCurrentFile].buffer;
|
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
|
}
|
|
|
|
|
gCurrentFile = name;
|
|
|
|
|
updateFiles();
|
|
|
|
|
gEditor.focus();
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
|
function updateFiles() {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
let files = document.getElementsByTagName('tf-files-pane')[0];
|
2023-05-03 19:12:34 -04:00
|
|
|
|
if (files) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
files.files = Object.fromEntries(
|
|
|
|
|
Object.keys(gFiles).map((file) => [
|
|
|
|
|
file,
|
|
|
|
|
{
|
|
|
|
|
clean:
|
|
|
|
|
(file == gCurrentFile
|
|
|
|
|
? gEditor.state.doc.toString()
|
|
|
|
|
: gFiles[file].doc.doc.toString()) == gFiles[file].original,
|
|
|
|
|
},
|
|
|
|
|
])
|
|
|
|
|
);
|
2023-05-03 19:12:34 -04:00
|
|
|
|
files.current = gCurrentFile;
|
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
|
gEditor.focus();
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} name
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
|
function makeNewFile(name) {
|
|
|
|
|
gFiles[name] = {
|
2024-01-13 12:40:47 -05:00
|
|
|
|
doc: cm6.EditorState.create({extensions: cm6.extensions}),
|
2021-01-02 13:10:00 -05:00
|
|
|
|
};
|
|
|
|
|
openFile(name);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
|
function newFile() {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
let name = prompt('Name of new file:', 'file.js');
|
2021-01-02 13:10:00 -05:00
|
|
|
|
if (name && !gFiles[name]) {
|
|
|
|
|
makeNewFile(name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
|
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
|
function removeFile() {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
if (confirm('Remove ' + gCurrentFile + '?')) {
|
2021-01-02 13:10:00 -05:00
|
|
|
|
delete gFiles[gCurrentFile];
|
|
|
|
|
openFile(Object.keys(gFiles)[0]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
|
|
|
|
*/
|
2024-01-21 18:56:36 -05:00
|
|
|
|
async function appExport() {
|
|
|
|
|
let JsZip = (await import('/static/jszip.min.js')).default;
|
|
|
|
|
let owner = window.location.pathname.split('/')[1].replace('~', '');
|
|
|
|
|
let name = window.location.pathname.split('/')[2];
|
|
|
|
|
let zip = new JsZip();
|
2024-02-24 11:09:34 -05:00
|
|
|
|
zip.file(
|
|
|
|
|
`${name}.json`,
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
type: 'tildefriends-app',
|
|
|
|
|
emoji: gApp.emoji || '📦',
|
|
|
|
|
})
|
|
|
|
|
);
|
2024-01-21 18:56:36 -05:00
|
|
|
|
for (let file of Object.keys(gFiles)) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
zip.file(
|
|
|
|
|
`${name}/${file}`,
|
|
|
|
|
gFiles[file].buffer ?? gFiles[file].doc.doc.toString()
|
|
|
|
|
);
|
2024-01-21 18:56:36 -05:00
|
|
|
|
}
|
|
|
|
|
let content = await zip.generateAsync({
|
2024-02-06 19:05:07 -05:00
|
|
|
|
type: 'base64',
|
2024-01-21 18:56:36 -05:00
|
|
|
|
compression: 'DEFLATE',
|
|
|
|
|
});
|
|
|
|
|
let a = document.createElement('a');
|
2024-02-06 19:05:07 -05:00
|
|
|
|
a.href = `data:application/zip;base64,${content}`;
|
2024-01-21 18:56:36 -05:00
|
|
|
|
a.download = `${owner}_${name}.zip`;
|
|
|
|
|
a.click();
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
|
* @param {*} name
|
|
|
|
|
* @param {*} file
|
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2024-01-21 18:56:36 -05:00
|
|
|
|
async function save_file_to_blob_id(name, file) {
|
|
|
|
|
console.log(`Saving ${name}.`);
|
|
|
|
|
let response = await fetch('/save', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/binary',
|
|
|
|
|
},
|
|
|
|
|
body: file,
|
|
|
|
|
});
|
|
|
|
|
if (!response.ok) {
|
2024-02-24 11:09:34 -05:00
|
|
|
|
throw new Error(
|
|
|
|
|
'Saving "' + name + '": ' + response.status + ' ' + response.statusText
|
|
|
|
|
);
|
2024-01-21 18:56:36 -05:00
|
|
|
|
}
|
|
|
|
|
let blob_id = await response.text();
|
|
|
|
|
if (blob_id.charAt(0) == '/') {
|
|
|
|
|
blob_id = blob_id.substr(1);
|
|
|
|
|
}
|
|
|
|
|
return blob_id;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
|
|
|
|
* TODOC
|
|
|
|
|
*/
|
2024-01-21 18:56:36 -05:00
|
|
|
|
async function appImport() {
|
|
|
|
|
let JsZip = (await import('/static/jszip.min.js')).default;
|
|
|
|
|
let input = document.createElement('input');
|
|
|
|
|
input.type = 'file';
|
|
|
|
|
input.click();
|
2024-02-24 11:09:34 -05:00
|
|
|
|
input.onchange = async function () {
|
2024-01-21 18:56:36 -05:00
|
|
|
|
try {
|
|
|
|
|
for (let file of input.files) {
|
|
|
|
|
if (file.type != 'application/zip') {
|
2024-02-06 19:05:07 -05:00
|
|
|
|
console.log(`This does not look like a .zip (${file.type}).`);
|
2024-01-21 18:56:36 -05:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-02-06 19:05:07 -05:00
|
|
|
|
let buffer = new Uint8Array(await file.arrayBuffer());
|
2024-02-24 11:09:34 -05:00
|
|
|
|
console.log(
|
|
|
|
|
'ZIP',
|
|
|
|
|
file.name,
|
|
|
|
|
file.type,
|
|
|
|
|
buffer,
|
|
|
|
|
buffer?.byteLength,
|
|
|
|
|
buffer?.length
|
|
|
|
|
);
|
2024-01-21 18:56:36 -05:00
|
|
|
|
let zip = new JsZip();
|
|
|
|
|
await zip.loadAsync(buffer);
|
|
|
|
|
let app_object;
|
|
|
|
|
let app_name;
|
|
|
|
|
for (let [name, object] of Object.entries(zip.files)) {
|
|
|
|
|
if (name.endsWith('.json') && name.indexOf('/') == -1) {
|
|
|
|
|
try {
|
|
|
|
|
let parsed = JSON.parse(await object.async('text'));
|
|
|
|
|
if (parsed.type == 'tildefriends-app') {
|
|
|
|
|
app_object = parsed;
|
|
|
|
|
app_name = name.substring(0, name.length - '.json'.length);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (app_object) {
|
|
|
|
|
app_object.files = {};
|
|
|
|
|
for (let [name, object] of Object.entries(zip.files)) {
|
|
|
|
|
if (!name.startsWith(app_name + '/') || name.endsWith('/')) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-02-24 11:09:34 -05:00
|
|
|
|
app_object.files[name.substring(app_name.length + '/'.length)] =
|
|
|
|
|
await save_file_to_blob_id(
|
|
|
|
|
name,
|
|
|
|
|
await object.async('arrayBuffer')
|
|
|
|
|
);
|
2024-01-21 18:56:36 -05:00
|
|
|
|
}
|
2024-02-24 11:09:34 -05:00
|
|
|
|
let path =
|
|
|
|
|
'/' +
|
|
|
|
|
(await save_file_to_blob_id(
|
|
|
|
|
`${app_name}.json`,
|
|
|
|
|
JSON.stringify(app_object)
|
|
|
|
|
)) +
|
|
|
|
|
'/';
|
2024-01-21 18:56:36 -05:00
|
|
|
|
console.log('Redirecting to:', path);
|
|
|
|
|
window.location.pathname = path;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
alert(e.toString());
|
|
|
|
|
}
|
2024-02-24 11:09:34 -05:00
|
|
|
|
};
|
2024-01-21 18:56:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
/**
|
2024-02-22 15:23:39 -05:00
|
|
|
|
*
|
2024-02-19 13:12:42 -05:00
|
|
|
|
*/
|
2024-02-17 13:53:21 -05:00
|
|
|
|
async function sourcePretty() {
|
|
|
|
|
let prettier = (await import('/prettier/standalone.mjs')).default;
|
|
|
|
|
let babel = (await import('/prettier/babel.mjs')).default;
|
|
|
|
|
let estree = (await import('/prettier/estree.mjs')).default;
|
|
|
|
|
let source = gEditor.state.doc.toString();
|
|
|
|
|
let formatted = await prettier.format(source, {
|
|
|
|
|
parser: 'babel',
|
|
|
|
|
plugins: [babel, estree],
|
2024-02-22 15:31:15 -05:00
|
|
|
|
trailingComma: 'es5',
|
2024-02-17 13:53:21 -05:00
|
|
|
|
useTabs: true,
|
2024-02-22 15:31:15 -05:00
|
|
|
|
semi: true,
|
|
|
|
|
singleQuote: true,
|
|
|
|
|
bracketSpacing: false,
|
2024-02-17 13:53:21 -05:00
|
|
|
|
});
|
|
|
|
|
if (source !== formatted) {
|
|
|
|
|
gEditor.dispatch({
|
|
|
|
|
changes: {
|
|
|
|
|
from: 0,
|
|
|
|
|
to: gEditor.state.doc.length,
|
|
|
|
|
insert: formatted,
|
2024-02-24 11:09:34 -05:00
|
|
|
|
},
|
2024-02-17 13:53:21 -05:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-25 22:31:31 -05:00
|
|
|
|
function toggleVisibleWhitespace() {
|
|
|
|
|
let editor_style = document.getElementById('editor_style');
|
|
|
|
|
/*
|
|
|
|
|
* There is likely a better way to do this, but stomping on the CSS was
|
|
|
|
|
* the easiest to wrangle at the time.
|
|
|
|
|
*/
|
|
|
|
|
if (editor_style.innerHTML.length) {
|
|
|
|
|
editor_style.innerHTML = '';
|
2024-03-13 20:23:48 -04:00
|
|
|
|
window.localStorage.setItem('visible_whitespace', '0');
|
2024-02-25 22:31:31 -05:00
|
|
|
|
} else {
|
|
|
|
|
editor_style.innerHTML = css`
|
|
|
|
|
.cm-trailingSpace {
|
|
|
|
|
background-color: unset !important;
|
|
|
|
|
}
|
|
|
|
|
.cm-highlightTab {
|
|
|
|
|
background-image: unset !important;
|
|
|
|
|
}
|
|
|
|
|
.cm-highlightSpace:before {
|
|
|
|
|
content: unset !important;
|
|
|
|
|
}
|
|
|
|
|
`;
|
2024-03-13 20:23:48 -04:00
|
|
|
|
window.localStorage.setItem('visible_whitespace', '1');
|
2024-02-25 22:31:31 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
|
// TODOC
|
2016-04-11 11:54:26 -04:00
|
|
|
|
window.addEventListener('load', function () {
|
2016-03-12 13:50:43 -05:00
|
|
|
|
window.addEventListener('hashchange', hashChange);
|
|
|
|
|
window.addEventListener('focus', focus);
|
|
|
|
|
window.addEventListener('blur', blur);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
window.addEventListener('message', message, false);
|
2016-05-07 07:07:54 -04:00
|
|
|
|
window.addEventListener('online', connectSocket);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
document.getElementById('name').value = window.location.pathname;
|
2023-01-28 17:44:45 -05:00
|
|
|
|
document
|
|
|
|
|
.getElementById('closeEditor')
|
|
|
|
|
.addEventListener('click', () => closeEditor());
|
|
|
|
|
document.getElementById('save').addEventListener('click', () => save());
|
2023-03-21 19:08:04 -04:00
|
|
|
|
document.getElementById('icon').addEventListener('click', () => changeIcon());
|
2024-02-24 11:09:34 -05:00
|
|
|
|
document
|
|
|
|
|
.getElementById('delete')
|
|
|
|
|
.addEventListener('click', () => deleteApp());
|
|
|
|
|
document
|
|
|
|
|
.getElementById('export')
|
|
|
|
|
.addEventListener('click', () => appExport());
|
|
|
|
|
document
|
|
|
|
|
.getElementById('import')
|
|
|
|
|
.addEventListener('click', () => appImport());
|
|
|
|
|
document
|
|
|
|
|
.getElementById('pretty')
|
|
|
|
|
.addEventListener('click', () => sourcePretty());
|
2024-02-25 22:31:31 -05:00
|
|
|
|
document
|
|
|
|
|
.getElementById('whitespace')
|
|
|
|
|
.addEventListener('click', () => toggleVisibleWhitespace());
|
2024-02-24 11:09:34 -05:00
|
|
|
|
document
|
|
|
|
|
.getElementById('trace_button')
|
|
|
|
|
.addEventListener('click', function (event) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
trace();
|
|
|
|
|
});
|
2021-01-02 13:10:00 -05:00
|
|
|
|
connectSocket(window.location.pathname);
|
2022-03-07 16:06:20 -05:00
|
|
|
|
|
|
|
|
|
if (window.localStorage.getItem('editing') == '1') {
|
|
|
|
|
edit();
|
|
|
|
|
} else {
|
|
|
|
|
closeEditor();
|
|
|
|
|
}
|
2024-03-13 20:23:48 -04:00
|
|
|
|
if (window.localStorage.getItem('visible_whitespace') == '1') {
|
|
|
|
|
toggleVisibleWhitespace();
|
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
|
});
|