2022-09-06 19:26:43 -04:00
|
|
|
import {LitElement, html} from './lit-all.min.js';
|
|
|
|
import * as tfutils from './tf-utils.js';
|
|
|
|
import * as tfrpc from '/static/tfrpc.js';
|
2022-10-15 14:22:13 -04:00
|
|
|
import {styles} from './tf-styles.js';
|
|
|
|
import Tribute from './tribute.esm.js';
|
2022-09-06 19:26:43 -04:00
|
|
|
|
|
|
|
class TfComposeElement extends LitElement {
|
|
|
|
static get properties() {
|
|
|
|
return {
|
|
|
|
whoami: {type: String},
|
|
|
|
users: {type: Object},
|
|
|
|
root: {type: String},
|
|
|
|
branch: {type: String},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-15 14:22:13 -04:00
|
|
|
static styles = styles;
|
|
|
|
|
2022-09-06 19:26:43 -04:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.users = {};
|
|
|
|
this.root = undefined;
|
|
|
|
this.branch = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
changed(event) {
|
|
|
|
let edit = this.renderRoot.getElementById('edit');
|
|
|
|
let preview = this.renderRoot.getElementById('preview');
|
|
|
|
preview.innerHTML = tfutils.markdown(edit.value);
|
|
|
|
}
|
|
|
|
|
2022-10-04 21:40:21 -04:00
|
|
|
paste(event) {
|
|
|
|
let self = this;
|
|
|
|
for(let item of event.clipboardData.items) {
|
|
|
|
if (item.type?.startsWith('image/')) {
|
|
|
|
let file = item.getAsFile();
|
|
|
|
if (!file) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
file.arrayBuffer().then(function(buffer) {
|
|
|
|
let bin = Array.from(new Uint8Array(buffer));
|
|
|
|
return tfrpc.rpc.store_blob(bin);
|
|
|
|
}).then(function(id) {
|
|
|
|
let edit = self.renderRoot.getElementById('edit');
|
|
|
|
edit.value += `\n![${file.name}](${id})`;
|
|
|
|
self.changed();
|
|
|
|
}).catch(function(e) {
|
|
|
|
alert(e.message);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 19:26:43 -04:00
|
|
|
submit() {
|
|
|
|
let self = this;
|
|
|
|
let edit = this.renderRoot.getElementById('edit');
|
|
|
|
let message = {
|
|
|
|
type: 'post',
|
|
|
|
text: edit.value,
|
|
|
|
};
|
|
|
|
if (this.root || this.branch) {
|
|
|
|
message.root = this.root;
|
|
|
|
message.branch = this.branch;
|
|
|
|
}
|
2022-10-15 15:02:09 -04:00
|
|
|
for (let match of message.text.matchAll(/\[([^\[]+)]\((@[^\)]+)/g)) {
|
|
|
|
if (!message.mentions) {
|
|
|
|
message.mentions = [];
|
|
|
|
}
|
|
|
|
let name = match[1].length;
|
|
|
|
let balance = 0;
|
|
|
|
let bracket_end = match.index + match[1].length + '[]'.length - 1;
|
|
|
|
for (let i = bracket_end; i >= 0; i--) {
|
|
|
|
if (message.text.charAt(i) == ']') {
|
|
|
|
balance++;
|
|
|
|
} else if (message.text.charAt(i) == '[') {
|
|
|
|
balance--;
|
|
|
|
}
|
|
|
|
if (balance <= 0) {
|
|
|
|
name = message.text.substring(i + 1, bracket_end - i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
message.mentions.push({
|
|
|
|
link: match[2],
|
|
|
|
name: name.charAt(0) == '@' ? name.substring(1) : name,
|
|
|
|
});
|
|
|
|
}
|
2022-09-06 19:26:43 -04:00
|
|
|
console.log('Would post:', message);
|
|
|
|
tfrpc.rpc.appendMessage(this.whoami, message).then(function() {
|
|
|
|
edit.value = '';
|
|
|
|
self.changed();
|
|
|
|
}).catch(function(error) {
|
|
|
|
alert(error.message);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
discard() {
|
|
|
|
let edit = this.renderRoot.getElementById('edit');
|
|
|
|
edit.value = '';
|
|
|
|
this.changed();
|
|
|
|
this.dispatchEvent(new CustomEvent('tf-discard'));
|
|
|
|
}
|
|
|
|
|
|
|
|
attach() {
|
|
|
|
let self = this;
|
|
|
|
let edit = this.renderRoot.getElementById('edit');
|
|
|
|
let input = document.createElement('input');
|
|
|
|
input.type = 'file';
|
|
|
|
input.onchange = function(event) {
|
|
|
|
let file = event.target.files[0];
|
|
|
|
file.arrayBuffer().then(function(buffer) {
|
|
|
|
let bin = Array.from(new Uint8Array(buffer));
|
|
|
|
return tfrpc.rpc.store_blob(bin);
|
|
|
|
}).then(function(id) {
|
|
|
|
edit.value += `\n![${file.name}](${id})`;
|
|
|
|
self.changed();
|
|
|
|
}).catch(function(e) {
|
|
|
|
alert(e.message);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
input.click();
|
|
|
|
}
|
|
|
|
|
2022-10-15 14:22:13 -04:00
|
|
|
firstUpdated() {
|
|
|
|
let tribute = new Tribute({
|
|
|
|
values: Object.entries(this.users).map(x => ({key: x[1].name, value: x[0]})),
|
|
|
|
selectTemplate: function(item) {
|
|
|
|
return `[@${item.original.key}](${item.original.value})`;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
tribute.attach(this.renderRoot.getElementById('edit'));
|
|
|
|
}
|
|
|
|
|
2022-09-06 19:26:43 -04:00
|
|
|
render() {
|
2022-10-15 14:22:13 -04:00
|
|
|
let result = html`
|
2022-09-06 19:26:43 -04:00
|
|
|
<div style="display: flex; flex-direction: row; width: 100%">
|
2022-10-04 21:40:21 -04:00
|
|
|
<textarea id="edit" @input=${this.changed} @paste=${this.paste} style="flex: 1 0 50%"></textarea>
|
2022-09-06 19:26:43 -04:00
|
|
|
<div id="preview" style="flex: 1 0 50%"></div>
|
|
|
|
</div>
|
|
|
|
<input type="button" value="Submit" @click=${this.submit}></input>
|
|
|
|
<input type="button" value="Attach" @click=${this.attach}></input>
|
|
|
|
<input type="button" value="Discard" @click=${this.discard}></input>
|
|
|
|
`;
|
2022-10-15 14:22:13 -04:00
|
|
|
return result;
|
2022-09-06 19:26:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
customElements.define('tf-compose', TfComposeElement);
|