The frontend/UI server, written in JS using the MarkoJS library
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.

129 lines
3.2 KiB

  1. const EventEmitter = require("events");
  2. let linkSignal = null;
  3. module.exports = class {
  4. onCreate(input) {
  5. this.state = {
  6. shortName: "",
  7. name: "",
  8. text: "",
  9. nameSuffix: "",
  10. modal: null,
  11. removed: false,
  12. multipart: false,
  13. anchored: false,
  14. prevWasText: false,
  15. }
  16. this.mounted = false;
  17. this.updatePost(input)
  18. }
  19. open(modal) {
  20. this.state.modal = modal
  21. }
  22. close() {
  23. this.state.modal = null
  24. }
  25. move(toPosition) {
  26. this.emit("move", toPosition)
  27. }
  28. remove() {
  29. this.state.removed = true
  30. this.emit("remove")
  31. }
  32. edited(data) {
  33. this.emit("edited", data)
  34. }
  35. onInput(input) {
  36. if (this.state == null) {
  37. return
  38. }
  39. const timediff = Date.parse(input.post.time) - Date.parse(input.prev.time)
  40. this.state.multipart = (input.post.nick == input.prev.nick && input.prev.multipart !== false) && (Math.abs(timediff) < 60000);
  41. this.state.prevWasText = (input.prev.kind === "text");
  42. this.updatePost(input)
  43. }
  44. onMount() {
  45. this.linkSignalCallback = () => {
  46. this.state.anchored = false
  47. }
  48. if (linkSignal == null) {
  49. linkSignal = new EventEmitter()
  50. linkSignal.setMaxListeners(10000)
  51. }
  52. linkSignal.on("signal", this.linkSignalCallback)
  53. this.state.anchored = window.location.hash.slice(1) === this.input.post.id;
  54. }
  55. onUnmount() {
  56. linkSignal.removeListener("signal", this.linkSignalCallback)
  57. }
  58. link() {
  59. linkSignal.emit("signal")
  60. this.state.anchored = true
  61. }
  62. updatePost(input) {
  63. this.state.shortName = input.post.nick.split("_").shift()
  64. this.state.name = input.post.nick
  65. this.state.nameSuffix = ""
  66. this.state.text = input.post.text.replace(/\x02/g, "**").replace(/\x1D/g, "*").replace(/\`/g, "\\`").trim();
  67. if (this.state.text.startsWith("|")) {
  68. this.state.name = ""
  69. this.state.text = this.state.text.slice(1).trim();
  70. }
  71. if (input.post.kind === "text" && !this.state.text.includes("\"") && !this.state.text.includes("\''") && !this.state.text.includes("|")) {
  72. const colonIndex = this.state.text.indexOf(": ");
  73. if (colonIndex != -1 && colonIndex < this.state.text.indexOf(" ")) {
  74. this.state.text = this.state.text.replace(": ", ": \"") + "\"";
  75. } else {
  76. this.state.text = '"' + this.state.text + '"'
  77. }
  78. }
  79. if (this.state.text.charAt(0) == this.state.text.charAt(0).toUpperCase()) {
  80. this.state.name = ""
  81. }
  82. if (!input.post.nick.startsWith("=")) {
  83. const postNick = input.post.nick.replace("'s", "").replace("s'", "s")
  84. for (const character of input.characters) {
  85. for (const nick of character.nicks) {
  86. if (nick === postNick) {
  87. this.state.name = character.name
  88. this.state.shortName = character.shortName
  89. if (input.post.nick.endsWith("'s") || input.post.nick.endsWith("'")) {
  90. this.state.nameSuffix = (character.shortName.endsWith("s") || character.shortName.endsWith("z")) ? "'" : "'s"
  91. }
  92. return
  93. }
  94. }
  95. }
  96. }
  97. }
  98. kindClass(prefix) {
  99. if (this.input.post == null) {
  100. return
  101. }
  102. return `${prefix}${this.input.post.kind.replace(".", "-")}`
  103. }
  104. }