You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
2.9 KiB
125 lines
2.9 KiB
const EventEmitter = require("events");
|
|
|
|
let linkSignal = null;
|
|
|
|
module.exports = class {
|
|
onCreate(input) {
|
|
this.state = {
|
|
shortName: "",
|
|
name: "",
|
|
text: "",
|
|
nameSuffix: "",
|
|
modal: null,
|
|
removed: false,
|
|
multipart: false,
|
|
anchored: false,
|
|
prevWasText: false,
|
|
dark: false,
|
|
}
|
|
|
|
this.mounted = false;
|
|
|
|
this.updatePost(input)
|
|
}
|
|
|
|
open(modal) {
|
|
this.state.modal = modal
|
|
}
|
|
|
|
close() {
|
|
this.state.modal = null
|
|
}
|
|
|
|
move(toPosition) {
|
|
this.emit("move", toPosition)
|
|
}
|
|
|
|
remove() {
|
|
this.state.removed = true
|
|
this.emit("remove")
|
|
}
|
|
|
|
edited(data) {
|
|
this.emit("edited", data)
|
|
}
|
|
|
|
onInput(input) {
|
|
if (this.state == null) {
|
|
return
|
|
}
|
|
|
|
const timediff = Date.parse(input.post.time) - Date.parse(input.prev.time)
|
|
this.state.multipart = (!input.post.kind.startsWith("annotation.") && input.post.nick == input.prev.nick && input.prev.multipart !== false) && (Math.abs(timediff) < 60000);
|
|
this.state.prevWasText = (input.prev.kind === "text");
|
|
|
|
this.updatePost(input)
|
|
}
|
|
|
|
onMount() {
|
|
this.linkSignalCallback = () => {
|
|
this.state.anchored = false
|
|
}
|
|
if (linkSignal == null) {
|
|
linkSignal = new EventEmitter()
|
|
linkSignal.setMaxListeners(10000)
|
|
}
|
|
linkSignal.on("signal", this.linkSignalCallback)
|
|
|
|
this.state.anchored = window.location.hash.slice(1) === this.input.post.id;
|
|
}
|
|
|
|
onUnmount() {
|
|
linkSignal.removeListener("signal", this.linkSignalCallback)
|
|
}
|
|
|
|
link() {
|
|
linkSignal.emit("signal")
|
|
this.state.anchored = true
|
|
}
|
|
|
|
updatePost(input) {
|
|
this.state.shortName = input.post.nick.split("_").shift()
|
|
this.state.name = input.post.nick
|
|
this.state.nameSuffix = ""
|
|
|
|
this.state.text = input.post.text
|
|
|
|
if (input.highlights.length > 0 && (input.post.kind === "action" || input.post.kind === "text")) {
|
|
this.state.dark = true
|
|
} else {
|
|
this.state.dark = false
|
|
}
|
|
|
|
if (!input.post.nick.startsWith("=")) {
|
|
const postNick = input.post.nick.replace("'s", "").replace("s'", "s")
|
|
for (const character of input.characters) {
|
|
for (const nick of character.nicks) {
|
|
if (nick === postNick) {
|
|
this.state.name = character.name + character.id
|
|
this.state.shortName = character.shortName
|
|
|
|
if (input.post.nick.endsWith("'s") || input.post.nick.endsWith("'")) {
|
|
this.state.nameSuffix = (character.shortName.endsWith("s") || character.shortName.endsWith("z")) ? "'" : "'s"
|
|
}
|
|
|
|
if (input.highlights.length > 0) {
|
|
this.state.dark = !(input.highlights.includes(character.id))
|
|
} else {
|
|
this.state.dark = false
|
|
}
|
|
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
kindClass(prefix) {
|
|
if (this.input.post == null) {
|
|
return
|
|
}
|
|
|
|
return `${prefix}${this.input.post.kind.replace(".", "-")}`
|
|
}
|
|
}
|