RichText.vue 812 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <template>
  2. <div class="rich-text"></div>
  3. </template>
  4. <script>
  5. import { renderRichText } from "./vEditor/renderJSON";
  6. export default {
  7. name: "RichText",
  8. props: {
  9. textJson: {
  10. type: [String, Object],
  11. default() {
  12. return { sections: [] };
  13. },
  14. },
  15. },
  16. data() {
  17. return {};
  18. },
  19. watch: {
  20. textJson(val) {
  21. this.renderVal(val);
  22. },
  23. },
  24. mounted() {
  25. this.renderVal(this.textJson);
  26. },
  27. methods: {
  28. getJson(val) {
  29. let json = null;
  30. try {
  31. json = JSON.parse(val);
  32. } catch (e) {
  33. console.log("not json");
  34. }
  35. return json;
  36. },
  37. renderVal(val) {
  38. if (typeof textJson === "string") {
  39. this.$el.innerHTML = val;
  40. } else {
  41. renderRichText(val, this.$el);
  42. }
  43. },
  44. },
  45. };
  46. </script>