|
1
|
|
|
import AbstractMenuItemDispatcher from './AbstractMenuItemDispatcher'; |
|
2
|
|
|
import MenuItem from '../MenuItem'; |
|
3
|
|
|
import SetDocAttrStep from '../../../custom/SetDocAttrStep'; |
|
4
|
|
|
import { svgIcon } from '../MDI'; |
|
5
|
|
|
|
|
6
|
|
|
export default class PageMacroMenuItemDispatcher extends AbstractMenuItemDispatcher { |
|
7
|
|
|
/** |
|
8
|
|
|
* Create an MenuItemDispatcher to toggle the state of a page macro |
|
9
|
|
|
* |
|
10
|
|
|
* @param {string} macro The page macro, should be either NOCACHE or NOTOC |
|
11
|
|
|
*/ |
|
12
|
|
|
constructor(macro) { |
|
13
|
|
|
super(); |
|
14
|
|
|
this.macro = macro; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
isAvailable({ nodes }) { |
|
18
|
|
|
return !!nodes.doc.attrs[this.macro.toLowerCase()]; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
getMenuItem({ nodes }) { |
|
22
|
|
|
if (!this.isAvailable({ nodes })) { |
|
23
|
|
|
throw new Error('Plugin block not available in schema!'); |
|
24
|
|
|
} |
|
25
|
|
|
const macroAttrName = this.macro.toLowerCase(); |
|
26
|
|
|
return new MenuItem({ |
|
27
|
|
|
command: (state, dispatch) => { |
|
28
|
|
|
if (dispatch) { |
|
29
|
|
|
const newState = !state.doc.attrs[macroAttrName]; |
|
30
|
|
|
const tr = state.tr; |
|
31
|
|
|
tr.step(new SetDocAttrStep(macroAttrName, newState)); |
|
32
|
|
|
dispatch(tr); |
|
33
|
|
|
} |
|
34
|
|
|
return true; |
|
35
|
|
|
}, |
|
36
|
|
|
render: (editorView) => { |
|
|
|
|
|
|
37
|
|
|
const label = LANG.plugins.prosemirror[`label:${macroAttrName}`]; |
|
|
|
|
|
|
38
|
|
|
const blankIcon = MenuItem.renderSVGIcon(svgIcon('checkbox-blank-outline'), label).addClass('is-false'); |
|
39
|
|
|
const checkedIcon = MenuItem.renderSVGIcon(svgIcon('checkbox-marked-outline'), label).addClass('is-true'); |
|
40
|
|
|
const dom = jQuery('<span>').addClass('menuitem'); |
|
41
|
|
|
dom.append(blankIcon); |
|
42
|
|
|
dom.append(checkedIcon); |
|
43
|
|
|
dom.append(jQuery('<span>') |
|
44
|
|
|
.addClass('menulabel') |
|
45
|
|
|
.text(label)); |
|
46
|
|
|
return dom.get(0); |
|
47
|
|
|
}, |
|
48
|
|
|
update: (editorView, dom) => { |
|
49
|
|
|
const $dom = jQuery(dom); |
|
50
|
|
|
const attrState = editorView.state.doc.attrs[macroAttrName]; |
|
51
|
|
|
$dom.find('.is-true').toggle(attrState); |
|
52
|
|
|
$dom.find('.is-false').toggle(!attrState); |
|
53
|
|
|
}, |
|
54
|
|
|
isActive: state => state.doc.attrs[macroAttrName], |
|
55
|
|
|
}); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.