]> Repos - mime-chat/commitdiff
fix ts messageItem
authorriqo <hernandeze2@xavier.edu>
Sat, 10 Oct 2020 16:33:53 +0000 (11:33 -0500)
committerriqo <hernandeze2@xavier.edu>
Sat, 10 Oct 2020 16:33:53 +0000 (11:33 -0500)
crate/src/utils.rs
src/components/UI/UIDetails/messageItem.vue
src/components/UI/index.vue
tsconfig.json

index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..b1d7929dc9c4bc172367246f2e6263041406443f 100644 (file)
@@ -0,0 +1,10 @@
+pub fn set_panic_hook() {
+    // When the `console_error_panic_hook` feature is enabled, we can call the
+    // `set_panic_hook` function at least once during initialization, and then
+    // we will get better error messages if our code ever panics.
+    //
+    // For more details see
+    // https://github.com/rustwasm/console_error_panic_hook#readme
+    #[cfg(feature = "console_error_panic_hook")]
+    console_error_panic_hook::set_once();
+}
index d6c572c07716ba562f98795abb09d5c7172becd9..aee763fe356d27537836e44535473ca1a3bc5dea 100644 (file)
@@ -54,7 +54,7 @@
 </template>
 
 <script lang="ts">
-import { defineComponent } from 'vue';
+import { defineComponent, computed, onMounted, ref } from 'vue';
  export default defineComponent({
   name: "MessageItem",
   props: {
@@ -63,73 +63,101 @@ import { defineComponent } from 'vue';
       required: true
     },
   },
-  data() {
-    return {
-      textArr: [],
-      signature: ""
-    };
-  },
-  computed: {
-    textFill() {
-      switch (this.item.modifier) {
-        case "ai":
-          return "#fff";
-        default:
-          return "#000";
+  setup(props) {
+      interface ProtectedRef {
+        readonly el: SVGElement | HTMLElement;
+        reference: any;
+        height: number | null;
       }
-    },
-    bubbleFill() {
-      switch (this.item.modifier) {
-        case "sf":
-          return "#61b4f4";
-        case "ai":
-          return "#585858";
-        default:
-          return "#fff";
+      let textArr: string[] = [];
+      let signature = "";
+      const bubble: ProtectedRef = {
+        el: document.createElement("rect"),
+        reference:  ref(null),
+        height: 0,
+      };
+      const content: ProtectedRef = {
+        el: document.createElement("text"),
+        reference:  ref(null),
+        height: 0,
       }
-    },
-    startingOffset() {
-      switch (this.item.modifier) {
-        case "sf":
-          return "translate(10, 600)";
-        case "ai":
-          return "translate(135, 600)";
-        default:
-          return "translate(20, 600)";
+
+      const textFill = computed(() => {
+        switch (props.item.modifier) {
+          case "ai":
+            return "#fff";
+          default:
+            return "#000";
+        }
+      });
+
+      const bubbleFill = computed(() => {
+        switch (props.item.modifier) {
+          case "sf":
+            return "#61b4f4";
+          case "ai":
+            return "#585858";
+          default:
+            return "#fff";
+        }
+      });
+
+      const startingOffset = computed(() => {
+        switch (props.item.modifier) {
+          case "sf":
+            return "translate(10, 600)";
+          case "ai":
+            return "translate(135, 600)";
+          default:
+            return "translate(20, 600)";
+        }
+      })
+
+      function signaturePosition(): void {
+          if (bubble.reference.value.getAttribute("height") !== null) {
+            const height = Number(bubble.reference.value.getAttribute("height"));
+            const p = -10;
+            signature = `translate(-10 ${height + p})`;
+          }
       }
-    }
-  },
-  async mounted() {
-    await this.createTextBox(this.item.content, 32);
-    await this.adjustRect();
-    this.signaturePosition();
+
+      function createTextBox(s: string, w: number): void {
+        // return tspan elements for text with correct size
+        const wrap = (s: string, w: number) =>
+          s.replace(
+            new RegExp(`(?![^\\n]{1,${w}}$)([^\\n]{1,${w}})\\s`, "g"),
+            "$1\n"
+          );
+        textArr = wrap(s, w).split("\n");
+      }
+
+      function adjustRect(): void {
+        // must get dimensions of text box to fit bubble around
+        const padding = 20;
+        const bbox = content.reference.value.getBBox();
+        bubble.reference.value.setAttribute("x", bbox.x - padding);
+        bubble.reference.value.setAttribute("y", bbox.y - padding);
+        bubble.reference.value.setAttribute("width", bbox.width + 2 * padding);
+        bubble.reference.value.setAttribute("height", bbox.height + 2 * padding);
+      }
+
+      onMounted(async () => {
+        await createTextBox(props.item.content, 32);
+        await adjustRect();
+        signaturePosition();
+      })
+
+      return {
+          bubble,
+          textFill,
+          bubbleFill,
+          startingOffset,
+          signaturePosition,
+          textArr,
+          signature
+      }
+
   },
-  methods: {
-    signaturePosition() {
-      const height = Number(this.$refs.bubble.getAttribute("height"));
-      const p = -10;
-      this.signature = `translate(-10 ${height + p})`;
-    },
-    createTextBox(s, w) {
-      // return tspan elements for text with correct size
-      const wrap = (s, w) =>
-        s.replace(
-          new RegExp(`(?![^\\n]{1,${w}}$)([^\\n]{1,${w}})\\s`, "g"),
-          "$1\n"
-        );
-      this.textArr = wrap(s, w).split("\n");
-    },
-    adjustRect() {
-      // must get dimensions of text box to fit bubble around
-      const padding = 20;
-      const bbox = this.$refs.content.getBBox();
-      const bubble = this.$refs.bubble;
-      bubble.setAttribute("x", bbox.x - padding);
-      bubble.setAttribute("y", bbox.y - padding);
-      bubble.setAttribute("width", bbox.width + 2 * padding);
-      bubble.setAttribute("height", bbox.height + 2 * padding);
-    }
-  }
  });
 </script>
 
index b62d09b223e1c838e94ff28321219a2f761e8327..722b7de5f324617bf39557854e69e09686bb87fd 100644 (file)
@@ -84,7 +84,7 @@
 </template>
 
 <script lang="ts">
-import { defineComponent, inject, ref, onMounted } from "vue";
+import { defineComponent, inject, onMounted } from "vue";
 // import MessageItem from './UIDetails/messageItem.vue';
 import Messages from "./UIDetails/messages";
  export default defineComponent({
@@ -100,12 +100,12 @@ import Messages from "./UIDetails/messages";
            x: null,
            y: null
        }
-       const renderMsg = (i: number) => {
+       function renderMsg(i: number): void {
          setTimeout(() => {
            renderArr.push(messages[i]);
          }, 2000);
        }
-       const runAnim = () => {
+       function runAnim(): void {
          for (let i = 0; i < messages.length; i++) {
              renderMsg(i);
          }
index fc67121bbfdbaef70cdb8242e2da1e11a3f1f65b..939bcacf75f5b0a596bf4e4e16d75d15cf7d8926 100644 (file)
@@ -11,6 +11,7 @@
     "allowSyntheticDefaultImports": true,
     "sourceMap": true,
     "baseUrl": ".",
+    "typeRoots": ["./node_modules/@types"],
     "types": [
       "webpack-env",
       "jest"