summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/content_editor/components/editor_state_observer.vue
blob: 2eeb0719096a16d798d312c2962ccffb56aa13e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<script>
import { debounce } from 'lodash';

export const tiptapToComponentMap = {
  update: 'docUpdate',
  selectionUpdate: 'selectionUpdate',
  transaction: 'transaction',
  focus: 'focus',
  blur: 'blur',
  error: 'error',
};

const getComponentEventName = (tiptapEventName) => tiptapToComponentMap[tiptapEventName];

export default {
  inject: ['tiptapEditor'],
  created() {
    this.disposables = [];

    Object.keys(tiptapToComponentMap).forEach((tiptapEvent) => {
      const eventHandler = debounce((params) => this.handleTipTapEvent(tiptapEvent, params), 100);

      this.tiptapEditor?.on(tiptapEvent, eventHandler);

      this.disposables.push(() => this.tiptapEditor?.off(tiptapEvent, eventHandler));
    });
  },
  beforeDestroy() {
    this.disposables.forEach((dispose) => dispose());
  },
  methods: {
    handleTipTapEvent(tiptapEvent, params) {
      this.$emit(getComponentEventName(tiptapEvent), params);
    },
  },
  render() {
    return this.$slots.default;
  },
};
</script>