All files / app/assets/javascripts/content_editor/extensions loading.js

100% Statements 26/26
83.33% Branches 5/6
100% Functions 10/10
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68        8x 16x 16x 16x 16x     8x     90x     202x 202x   202x 16x       16x 186x 11x 8x     202x         521x         8x 11x 11x   11x     8x               1558x               90x      
import { Node } from '@tiptap/core';
import { Decoration, DecorationSet } from '@tiptap/pm/view';
import { Plugin } from '@tiptap/pm/state';
 
const createDotsLoader = () => {
  const root = document.createElement('span');
  root.classList.add('gl-display-inline-flex', 'gl-align-items-center');
  root.innerHTML = '<span class="gl-dots-loader gl-mx-2"><span></span></span>';
  return root;
};
 
export const loadingPlugin = new Plugin({
  state: {
    init() {
      return DecorationSet.empty;
    },
    apply(tr, set) {
      let transformedSet = set.map(tr.mapping, tr.doc);
      const action = tr.getMeta(this);
 
      if (action?.add) {
        const deco = Decoration.widget(action.add.pos, createDotsLoader(), {
          id: action.add.loaderId,
          side: -1,
        });
        transformedSet = transformedSet.add(tr.doc, [deco]);
      } else if (action?.remove) {
        transformedSet = transformedSet.remove(
          transformedSet.find(null, null, (spec) => spec.id === action.remove.loaderId),
        );
      }
      return transformedSet;
    },
  },
  props: {
    decorations(state) {
      return this.getState(state);
    },
  },
});
 
export const findLoader = (state, loaderId) => {
  const decos = loadingPlugin.getState(state);
  const found = decos.find(null, null, (spec) => spec.id === loaderId);
 
  return found.length ? found[0].from : null;
};
 
export const findAllLoaders = (state) => loadingPlugin.getState(state).find();
 
export default Node.create({
  name: 'loading',
  inline: true,
  group: 'inline',
 
  addAttributes() {
    return {
      id: {
        default: null,
      },
    };
  },
 
  addProseMirrorPlugins() {
    return [loadingPlugin];
  },
});