Welcome to mirror list, hosted at ThFree Co, Russian Federation.

loading.js « extensions « content_editor « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 942ac650925816559e3cb4a72c819f342c79938c (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
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
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];
  },
});