Completed
Push — master ( 3b8b0c...578b04 )
by Michael
02:39
created

script/main.js   A

Complexity

Total Complexity 7
Complexity/F 1.75

Size

Lines of Code 61
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 13
Bugs 0 Features 0
Metric Value
cc 0
eloc 41
c 13
b 0
f 0
nc 4
dl 0
loc 61
rs 10
wmc 7
mnd 1
bc 7
fnc 4
bpm 1.75
cpm 1.75
noi 2

2 Functions

Rating   Name   Duplication   Size   Complexity  
A main.js ➔ enableProsemirror 0 38 1
A main.js ➔ destroyProsemirror 0 6 3
1
import { EditorState } from 'prosemirror-state';
2
import { EditorView } from 'prosemirror-view';
3
import { Node } from 'prosemirror-model';
4
5
import schema from './schema';
6
import getKeymapPlugin from './plugins/Keymap/keymap';
7
import initializePublicAPI from './initializePublicAPI';
8
import MenuInitializer from './plugins/Menu/MenuInitializer';
9
import getNodeViews from './nodeviews/index';
10
11
initializePublicAPI();
12
13
const mi = new MenuInitializer(schema);
14
15
window.Prosemirror = window.Prosemirror || {};
16
17
window.Prosemirror.enableProsemirror = function enableProsemirror() {
18
    // PLUGIN ORDER IS IMPORTANT!
19
    const plugins = [
20
        mi.getMenuPlugin(),
21
        getKeymapPlugin(schema),
0 ignored issues
show
Bug introduced by
The variable seems to be never declared. If this is a global, consider adding a /** global: */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
22
    ];
23
24
    const json = jQuery('#dw__editform').find('[name=prosemirror_json]').get(0);
25
    const view = new EditorView(document.querySelector('#prosemirror__editor'), {
26
        state: EditorState.create({
27
            doc: Node.fromJSON(schema, JSON.parse(json.value)),
28
            schema,
29
            plugins,
30
        }),
31
        dispatchTransaction(tr) {
32
            console.log('run');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
33
34
            view.updateState(view.state.apply(tr));
35
36
            const spaces = 4;
37
            json.value = JSON.stringify(view.state.doc.toJSON(), null, spaces); // FIXME: no need to pretty print this!
38
        },
39
        nodeViews: getNodeViews(),
40
    });
41
    window.view = view;
42
    jQuery(window).on('scroll.prosemirror_menu', () => {
43
        const $container = jQuery('#prosemirror__editor');
44
        const $menuBar = $container.find('.menubar');
45
        const docViewTop = jQuery(window).scrollTop();
46
        const containerTop = $container.offset().top;
47
48
        if (docViewTop > containerTop) {
49
            $menuBar.css('position', 'fixed');
50
        } else {
51
            $menuBar.css('position', '');
52
        }
53
    });
54
};
55
56
window.Prosemirror.destroyProsemirror = function destroyProsemirror() {
57
    if (window.view && typeof window.view.destroy === 'function') {
58
        window.view.destroy();
59
    }
60
    jQuery(window).off('scroll.prosemirror_menu');
61
};
62