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; |
31
|
|
|
tr.step(new SetDocAttrStep(macroAttrName, newState)); |
32
|
|
|
dispatch(tr); |
33
|
|
|
} |
34
|
|
|
return true; |
35
|
|
|
}, |
36
|
|
|
render: () => { |
37
|
|
|
const label = LANG.plugins.prosemirror[`label:${macroAttrName}`]; |
|
|
|
|
38
|
|
|
const blankIcon = MenuItem.renderSVGIcon(svgIcon('checkbox-blank-outline'), label) |
39
|
|
|
.addClass('is-false'); |
40
|
|
|
const checkedIcon = MenuItem.renderSVGIcon(svgIcon('checkbox-marked-outline'), label) |
41
|
|
|
.addClass('is-true'); |
42
|
|
|
const dom = jQuery('<span>').addClass('menuitem'); |
43
|
|
|
dom.append(blankIcon); |
44
|
|
|
dom.append(checkedIcon); |
45
|
|
|
dom.append(jQuery('<span>') |
46
|
|
|
.addClass('menulabel') |
47
|
|
|
.text(label)); |
48
|
|
|
return dom.get(0); |
49
|
|
|
}, |
50
|
|
|
update: (editorView, dom) => { |
51
|
|
|
const $dom = jQuery(dom); |
52
|
|
|
const attrState = editorView.state.doc.attrs[macroAttrName]; |
53
|
|
|
$dom.find('.is-true').toggle(attrState); |
54
|
|
|
$dom.find('.is-false').toggle(!attrState); |
55
|
|
|
}, |
56
|
|
|
isActive: state => state.doc.attrs[macroAttrName], |
57
|
|
|
}); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
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.