prosemirror-menu 0.17.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -29,42 +29,25 @@ is the place to report issues.
29
29
  When using this module, you should make sure its `style/menu.css` file
30
30
  is loaded into your page.
31
31
 
32
- ### class MenuBarEditorView
32
+ ### menuBar
33
33
 
34
- A wrapper around
35
- [`EditorView`](http://prosemirror.net/ref.html#view.EditorView) that
36
- adds a menubar above the editor.
34
+ `menuBar(options) Plugin`
37
35
 
38
- Supports the following additional props:
36
+ A function that returns a plugin that causes an
37
+ [`EditorView`](http://prosemirror.net/ref.html#view.EditorView) to
38
+ show a menubar above the content.
39
39
 
40
- - **`floatingMenu`**`: ?bool`\
40
+ Supports the following options:
41
+
42
+ - **`floating`**`: ?bool`\
41
43
  Determines whether the menu floats, i.e. whether it sticks to the
42
44
  top of the viewport when the editor is partially scrolled out of
43
45
  view.
44
46
 
45
- - **`menuContent`**`: \[\[MenuElement]]`\
47
+ - **`content`**`: \[\[MenuElement]]`\
46
48
  Provides the content of the menu, as a nested array to be passed to
47
49
  `renderGrouped`.
48
50
 
49
- And has these properties and methods:
50
-
51
- - **`wrapper`**`: dom.Node`\
52
- The wrapping DOM element around the editor and the menu. Will get
53
- the CSS class `ProseMirror-menubar-wrapper`.
54
-
55
- - **`editor`**`: EditorView`\
56
- The wrapped editor view. _Don't_ directly call `update` or
57
- `updateState` on this, always go through the wrapping view.
58
-
59
- - **`props`**`: EditorProps`\
60
- The current props of this view.
61
-
62
- - **`update`**`(props: EditorProps)`\
63
- Update the view's props.
64
-
65
- - **`updateState`**`(state: EditorState)`\
66
- Update only the state of the editor.
67
-
68
51
  ### interface MenuElement
69
52
 
70
53
  The types defined in this module aren't the only thing you can display
@@ -94,14 +77,14 @@ An icon or label that, when clicked, executes a command.
94
77
 
95
78
  The configuration object passed to the `MenuItem` constructor.
96
79
 
97
- * `run(EditorState, fn(Action), EditorView)`\
80
+ * `run(EditorState, fn(Action), EditorView, dom.Event)`\
98
81
  The function to execute when the menu item is activated.
99
82
 
100
83
  * `select(EditorState) → bool`\
101
84
  Optional function that is used to determine whether the item is
102
85
  appropriate at the moment.
103
86
 
104
- * `onDeselect: ?string`\
87
+ * `onDeselected: ?string`\
105
88
  Determines what happens when `select`
106
89
  returns false. The default is to hide the item, you can set this to
107
90
  `"disable"` to instead render the item with a disabled style.
package/dist/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  ;var assign;
2
- ((assign = require("./menu"), exports.MenuItem = assign.MenuItem, exports.Dropdown = assign.Dropdown, exports.DropdownSubmenu = assign.DropdownSubmenu, exports.renderGrouped = assign.renderGrouped, exports.icons = assign.icons, exports.joinUpItem = assign.joinUpItem, exports.liftItem = assign.liftItem, exports.selectParentNodeItem = assign.selectParentNodeItem, exports.undoItem = assign.undoItem, exports.redoItem = assign.redoItem, exports.wrapItem = assign.wrapItem, exports.blockTypeItem = assign.blockTypeItem))
3
- exports.MenuBarEditorView = require("./menubar").MenuBarEditorView
2
+ ((assign = require("./menu"), exports.MenuItem = assign.MenuItem, exports.Dropdown = assign.Dropdown, exports.DropdownSubmenu = assign.DropdownSubmenu, exports.renderGrouped = assign.renderGrouped, exports.icons = assign.icons, exports.joinUpItem = assign.joinUpItem, exports.liftItem = assign.liftItem, exports.selectParentNodeItem = assign.selectParentNodeItem, exports.undoItem = assign.undoItem, exports.redoItem = assign.redoItem, exports.wrapItem = assign.wrapItem, exports.blockTypeItem = assign.blockTypeItem, assign))
3
+ exports.menuBar = require("./menubar").menuBar
4
4
 
5
5
  // !! This module defines a number of building blocks for ProseMirror
6
- // menus, along with a [menu bar](#menu.MenuBarEditorView) implementation.
6
+ // menus, along with a [menu bar](#menu.menuBar) implementation.
7
7
 
8
8
  // MenuElement:: interface
9
9
  // The types defined in this module aren't the only thing you can
package/dist/menu.js CHANGED
@@ -51,27 +51,27 @@ MenuItem.prototype.render = function render (view) {
51
51
  if (spec.css) { dom.style.cssText += spec.css }
52
52
  if (!disabled) { dom.addEventListener(spec.execEvent || "mousedown", function (e) {
53
53
  e.preventDefault()
54
- spec.run(view.state, view.dispatch, view)
54
+ spec.run(view.state, view.dispatch, view, e)
55
55
  }) }
56
56
  return dom
57
57
  };
58
58
  exports.MenuItem = MenuItem
59
59
 
60
60
  function translate(view, text) {
61
- return view.props.translate ? view.props.translate(text) : text
61
+ return view._props.translate ? view._props.translate(text) : text
62
62
  }
63
63
 
64
64
  // MenuItemSpec:: interface
65
65
  // The configuration object passed to the `MenuItem` constructor.
66
66
  //
67
- // run:: (EditorState, (Transaction), EditorView)
67
+ // run:: (EditorState, (Transaction), EditorView, dom.Event)
68
68
  // The function to execute when the menu item is activated.
69
69
  //
70
70
  // select:: ?(EditorState) → bool
71
71
  // Optional function that is used to determine whether the item is
72
72
  // appropriate at the moment.
73
73
  //
74
- // onDeselect:: ?string
74
+ // onDeselected:: ?string
75
75
  // Determines what happens when [`select`](#menu.MenuItemSpec.select)
76
76
  // returns false. The default is to hide the item, you can set this to
77
77
  // `"disable"` to instead render the item with a disabled style.
package/dist/menubar.js CHANGED
@@ -1,57 +1,59 @@
1
1
  var crel = require("crel")
2
- var ref = require("prosemirror-view");
3
- var EditorView = ref.EditorView;
2
+ var ref = require("prosemirror-state");
3
+ var Plugin = ref.Plugin;
4
4
 
5
5
  var ref$1 = require("./menu");
6
6
  var renderGrouped = ref$1.renderGrouped;
7
7
 
8
8
  var prefix = "ProseMirror-menubar"
9
9
 
10
- // ::- A wrapper around
11
- // [`EditorView`](http://prosemirror.net/ref.html#view.EditorView)
12
- // that adds a menubar above the editor.
10
+ // :: (Object)
11
+ // A plugin that will place a menu bar above the editor. Note that
12
+ // this involves wrapping the editor in an additional `<div>`.
13
13
  //
14
- // Supports the following additional props:
14
+ // options::-
15
+ // Supports the following options:
15
16
  //
16
- // - **`floatingMenu`**`: ?bool` determines whether the menu floats,
17
- // i.e. whether it sticks to the top of the viewport when the editor
18
- // is partially scrolled out of view.
17
+ // content:: [[MenuElement]]
18
+ // Provides the content of the menu, as a nested array to be
19
+ // passed to `renderGrouped`.
19
20
  //
20
- // - **`menuContent`**`: [[MenuElement]]` provides the content of the
21
- // menu, as a nested array to be passed to `renderGrouped`.
22
- var MenuBarEditorView = function MenuBarEditorView(place, props) {
21
+ // floating:: ?bool
22
+ // Determines whether the menu floats, i.e. whether it sticks to
23
+ // the top of the viewport when the editor is partially scrolled
24
+ // out of view.
25
+ function menuBar(options) {
26
+ return new Plugin({
27
+ view: function view(editorView) { return new MenuBarView(editorView, options) }
28
+ })
29
+ }
30
+ exports.menuBar = menuBar
31
+
32
+ var MenuBarView = function MenuBarView(editorView, options) {
23
33
  var this$1 = this;
24
34
 
25
- // :: dom.Node The wrapping DOM element around the editor and the
26
- // menu. Will get the CSS class `ProseMirror-menubar-wrapper`.
35
+ this.editorView = editorView
36
+ this.options = options
37
+
27
38
  this.wrapper = crel("div", {class: prefix + "-wrapper"})
28
- if (place && place.appendChild) { place.appendChild(this.wrapper) }
29
- else if (place) { place(this.wrapper) }
30
- if (!props.dispatchTransaction)
31
- { props.dispatchTransaction = function (tr) { return this$1.updateState(this$1.editor.state.apply(tr)); } }
32
- // :: EditorView The wrapped editor view. _Don't_ directly call
33
- // `update` or `updateState` on this, always go through the
34
- // wrapping view.
35
- this.editor = new EditorView(this.wrapper, props)
36
-
37
- this.menu = crel("div", {class: prefix})
39
+ this.menu = this.wrapper.appendChild(crel("div", {class: prefix}))
38
40
  this.menu.className = prefix
39
41
  this.spacer = null
40
42
 
41
- this.wrapper.insertBefore(this.menu, this.wrapper.firstChild)
43
+ editorView.dom.parentNode.replaceChild(this.wrapper, editorView.dom)
44
+ this.wrapper.appendChild(editorView.dom)
42
45
 
43
46
  this.maxHeight = 0
44
47
  this.widthForMaxHeight = 0
45
48
  this.floating = false
46
49
 
47
- // :: EditorProps The current props of this view.
48
- this.props = props
49
- this.updateMenu()
50
+ this.update()
50
51
 
51
- if (this.editor.someProp("floatingMenu")) {
52
+ if (options.floating) {
52
53
  this.updateFloat()
53
54
  this.scrollFunc = function () {
54
- if (!this$1.editor.root.contains(this$1.wrapper))
55
+ var root = this$1.editorView.root
56
+ if (!(root.body || root).contains(this$1.wrapper))
55
57
  { window.removeEventListener("scroll", this$1.scrollFunc) }
56
58
  else
57
59
  { this$1.updateFloat() }
@@ -60,22 +62,9 @@ var MenuBarEditorView = function MenuBarEditorView(place, props) {
60
62
  }
61
63
  };
62
64
 
63
- // :: (EditorProps) Update the view's props.
64
- MenuBarEditorView.prototype.update = function update (props) {
65
- this.props = props
66
- this.editor.update(props)
67
- this.updateMenu()
68
- };
69
-
70
- // :: (EditorState) Update only the state of the editor.
71
- MenuBarEditorView.prototype.updateState = function updateState (state) {
72
- this.editor.updateState(state)
73
- this.updateMenu()
74
- };
75
-
76
- MenuBarEditorView.prototype.updateMenu = function updateMenu () {
65
+ MenuBarView.prototype.update = function update () {
77
66
  this.menu.textContent = ""
78
- this.menu.appendChild(renderGrouped(this.editor, this.editor.someProp("menuContent")))
67
+ this.menu.appendChild(renderGrouped(this.editorView, this.options.content))
79
68
 
80
69
  if (this.floating) {
81
70
  this.updateScrollCursor()
@@ -91,9 +80,8 @@ MenuBarEditorView.prototype.updateMenu = function updateMenu () {
91
80
  }
92
81
  };
93
82
 
94
-
95
- MenuBarEditorView.prototype.updateScrollCursor = function updateScrollCursor () {
96
- var selection = this.editor.root.getSelection()
83
+ MenuBarView.prototype.updateScrollCursor = function updateScrollCursor () {
84
+ var selection = this.editorView.root.getSelection()
97
85
  if (!selection.focusNode) { return }
98
86
  var rects = selection.getRangeAt(0).getClientRects()
99
87
  var selRect = rects[selectionIsInverted(selection) ? 0 : rects.length - 1]
@@ -105,7 +93,7 @@ MenuBarEditorView.prototype.updateScrollCursor = function updateScrollCursor ()
105
93
  }
106
94
  };
107
95
 
108
- MenuBarEditorView.prototype.updateFloat = function updateFloat () {
96
+ MenuBarView.prototype.updateFloat = function updateFloat () {
109
97
  var parent = this.wrapper, editorRect = parent.getBoundingClientRect()
110
98
  if (this.floating) {
111
99
  if (editorRect.top >= 0 || editorRect.bottom < this.menu.offsetHeight + 10) {
@@ -132,12 +120,10 @@ MenuBarEditorView.prototype.updateFloat = function updateFloat () {
132
120
  }
133
121
  };
134
122
 
135
- // :: ()
136
- // Destroy the editor instance.
137
- MenuBarEditorView.prototype.destroy = function destroy () {
138
- this.editor.destroy()
123
+ MenuBarView.prototype.destroy = function destroy () {
124
+ if (this.wrapper.parentNode)
125
+ { this.wrapper.parentNode.replaceChild(this.editorView.dom, this.wrapper) }
139
126
  };
140
- exports.MenuBarEditorView = MenuBarEditorView
141
127
 
142
128
  // Not precise, but close enough
143
129
  function selectionIsInverted(selection) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prosemirror-menu",
3
- "version": "0.17.0",
3
+ "version": "0.20.0",
4
4
  "description": "Simple menu elements for ProseMirror",
5
5
  "main": "dist/index.js",
6
6
  "style": "style/menu.css",
@@ -18,9 +18,9 @@
18
18
  },
19
19
  "dependencies": {
20
20
  "crel": "^3.0.0",
21
- "prosemirror-view": "^0.17.0",
22
- "prosemirror-commands": "^0.17.0",
23
- "prosemirror-history": "^0.17.0"
21
+ "prosemirror-state": "^0.20.0",
22
+ "prosemirror-commands": "^0.20.0",
23
+ "prosemirror-history": "^0.20.0"
24
24
  },
25
25
  "devDependencies": {
26
26
  "buble": "^0.15.1",
package/src/README.md CHANGED
@@ -3,6 +3,7 @@
3
3
  @MenuItemSpec
4
4
  @Dropdown
5
5
  @DropdownSubmenu
6
+ @menuBar
6
7
 
7
8
  This module exports the following pre-built items or item
8
9
  constructors:
package/src/index.js CHANGED
@@ -3,10 +3,10 @@
3
3
  liftItem: exports.liftItem, selectParentNodeItem: exports.selectParentNodeItem,
4
4
  undoItem: exports.undoItem, redoItem: exports.redoItem, wrapItem: exports.wrapItem,
5
5
  blockTypeItem: exports.blockTypeItem} = require("./menu"))
6
- exports.MenuBarEditorView = require("./menubar").MenuBarEditorView
6
+ exports.menuBar = require("./menubar").menuBar
7
7
 
8
8
  // !! This module defines a number of building blocks for ProseMirror
9
- // menus, along with a [menu bar](#menu.MenuBarEditorView) implementation.
9
+ // menus, along with a [menu bar](#menu.menuBar) implementation.
10
10
 
11
11
  // MenuElement:: interface
12
12
  // The types defined in this module aren't the only thing you can
package/src/menu.js CHANGED
@@ -45,7 +45,7 @@ class MenuItem {
45
45
  if (spec.css) dom.style.cssText += spec.css
46
46
  if (!disabled) dom.addEventListener(spec.execEvent || "mousedown", e => {
47
47
  e.preventDefault()
48
- spec.run(view.state, view.dispatch, view)
48
+ spec.run(view.state, view.dispatch, view, e)
49
49
  })
50
50
  return dom
51
51
  }
@@ -53,20 +53,20 @@ class MenuItem {
53
53
  exports.MenuItem = MenuItem
54
54
 
55
55
  function translate(view, text) {
56
- return view.props.translate ? view.props.translate(text) : text
56
+ return view._props.translate ? view._props.translate(text) : text
57
57
  }
58
58
 
59
59
  // MenuItemSpec:: interface
60
60
  // The configuration object passed to the `MenuItem` constructor.
61
61
  //
62
- // run:: (EditorState, (Transaction), EditorView)
62
+ // run:: (EditorState, (Transaction), EditorView, dom.Event)
63
63
  // The function to execute when the menu item is activated.
64
64
  //
65
65
  // select:: ?(EditorState) → bool
66
66
  // Optional function that is used to determine whether the item is
67
67
  // appropriate at the moment.
68
68
  //
69
- // onDeselect:: ?string
69
+ // onDeselected:: ?string
70
70
  // Determines what happens when [`select`](#menu.MenuItemSpec.select)
71
71
  // returns false. The default is to hide the item, you can set this to
72
72
  // `"disable"` to instead render the item with a disabled style.
package/src/menubar.js CHANGED
@@ -1,54 +1,56 @@
1
1
  const crel = require("crel")
2
- const {EditorView} = require("prosemirror-view")
2
+ const {Plugin} = require("prosemirror-state")
3
3
 
4
4
  const {renderGrouped} = require("./menu")
5
5
 
6
6
  const prefix = "ProseMirror-menubar"
7
7
 
8
- // ::- A wrapper around
9
- // [`EditorView`](http://prosemirror.net/ref.html#view.EditorView)
10
- // that adds a menubar above the editor.
8
+ // :: (Object)
9
+ // A plugin that will place a menu bar above the editor. Note that
10
+ // this involves wrapping the editor in an additional `<div>`.
11
11
  //
12
- // Supports the following additional props:
12
+ // options::-
13
+ // Supports the following options:
13
14
  //
14
- // - **`floatingMenu`**`: ?bool` determines whether the menu floats,
15
- // i.e. whether it sticks to the top of the viewport when the editor
16
- // is partially scrolled out of view.
15
+ // content:: [[MenuElement]]
16
+ // Provides the content of the menu, as a nested array to be
17
+ // passed to `renderGrouped`.
17
18
  //
18
- // - **`menuContent`**`: [[MenuElement]]` provides the content of the
19
- // menu, as a nested array to be passed to `renderGrouped`.
20
- class MenuBarEditorView {
21
- constructor(place, props) {
22
- // :: dom.Node The wrapping DOM element around the editor and the
23
- // menu. Will get the CSS class `ProseMirror-menubar-wrapper`.
19
+ // floating:: ?bool
20
+ // Determines whether the menu floats, i.e. whether it sticks to
21
+ // the top of the viewport when the editor is partially scrolled
22
+ // out of view.
23
+ function menuBar(options) {
24
+ return new Plugin({
25
+ view(editorView) { return new MenuBarView(editorView, options) }
26
+ })
27
+ }
28
+ exports.menuBar = menuBar
29
+
30
+ class MenuBarView {
31
+ constructor(editorView, options) {
32
+ this.editorView = editorView
33
+ this.options = options
34
+
24
35
  this.wrapper = crel("div", {class: prefix + "-wrapper"})
25
- if (place && place.appendChild) place.appendChild(this.wrapper)
26
- else if (place) place(this.wrapper)
27
- if (!props.dispatchTransaction)
28
- props.dispatchTransaction = tr => this.updateState(this.editor.state.apply(tr))
29
- // :: EditorView The wrapped editor view. _Don't_ directly call
30
- // `update` or `updateState` on this, always go through the
31
- // wrapping view.
32
- this.editor = new EditorView(this.wrapper, props)
33
-
34
- this.menu = crel("div", {class: prefix})
36
+ this.menu = this.wrapper.appendChild(crel("div", {class: prefix}))
35
37
  this.menu.className = prefix
36
38
  this.spacer = null
37
39
 
38
- this.wrapper.insertBefore(this.menu, this.wrapper.firstChild)
40
+ editorView.dom.parentNode.replaceChild(this.wrapper, editorView.dom)
41
+ this.wrapper.appendChild(editorView.dom)
39
42
 
40
43
  this.maxHeight = 0
41
44
  this.widthForMaxHeight = 0
42
45
  this.floating = false
43
46
 
44
- // :: EditorProps The current props of this view.
45
- this.props = props
46
- this.updateMenu()
47
+ this.update()
47
48
 
48
- if (this.editor.someProp("floatingMenu")) {
49
+ if (options.floating) {
49
50
  this.updateFloat()
50
51
  this.scrollFunc = () => {
51
- if (!this.editor.root.contains(this.wrapper))
52
+ let root = this.editorView.root
53
+ if (!(root.body || root).contains(this.wrapper))
52
54
  window.removeEventListener("scroll", this.scrollFunc)
53
55
  else
54
56
  this.updateFloat()
@@ -57,22 +59,9 @@ class MenuBarEditorView {
57
59
  }
58
60
  }
59
61
 
60
- // :: (EditorProps) Update the view's props.
61
- update(props) {
62
- this.props = props
63
- this.editor.update(props)
64
- this.updateMenu()
65
- }
66
-
67
- // :: (EditorState) Update only the state of the editor.
68
- updateState(state) {
69
- this.editor.updateState(state)
70
- this.updateMenu()
71
- }
72
-
73
- updateMenu() {
62
+ update() {
74
63
  this.menu.textContent = ""
75
- this.menu.appendChild(renderGrouped(this.editor, this.editor.someProp("menuContent")))
64
+ this.menu.appendChild(renderGrouped(this.editorView, this.options.content))
76
65
 
77
66
  if (this.floating) {
78
67
  this.updateScrollCursor()
@@ -88,9 +77,8 @@ class MenuBarEditorView {
88
77
  }
89
78
  }
90
79
 
91
-
92
80
  updateScrollCursor() {
93
- let selection = this.editor.root.getSelection()
81
+ let selection = this.editorView.root.getSelection()
94
82
  if (!selection.focusNode) return
95
83
  let rects = selection.getRangeAt(0).getClientRects()
96
84
  let selRect = rects[selectionIsInverted(selection) ? 0 : rects.length - 1]
@@ -129,13 +117,11 @@ class MenuBarEditorView {
129
117
  }
130
118
  }
131
119
 
132
- // :: ()
133
- // Destroy the editor instance.
134
120
  destroy() {
135
- this.editor.destroy()
121
+ if (this.wrapper.parentNode)
122
+ this.wrapper.parentNode.replaceChild(this.editorView.dom, this.wrapper)
136
123
  }
137
124
  }
138
- exports.MenuBarEditorView = MenuBarEditorView
139
125
 
140
126
  // Not precise, but close enough
141
127
  function selectionIsInverted(selection) {
package/style/menu.css CHANGED
@@ -31,10 +31,12 @@
31
31
  .ProseMirror-menu-dropdown {
32
32
  vertical-align: 1px;
33
33
  cursor: pointer;
34
+ position: relative;
35
+ padding-right: 15px;
34
36
  }
35
37
 
36
38
  .ProseMirror-menu-dropdown-wrap {
37
- padding: 1px 14px 1px 4px;
39
+ padding: 1px 0 1px 4px;
38
40
  display: inline-block;
39
41
  position: relative;
40
42
  }
@@ -46,7 +48,7 @@
46
48
  border-top: 4px solid currentColor;
47
49
  opacity: .6;
48
50
  position: absolute;
49
- right: 2px;
51
+ right: 4px;
50
52
  top: calc(50% - 2px);
51
53
  }
52
54
 
@@ -137,6 +139,10 @@
137
139
  cursor: pointer;
138
140
  }
139
141
 
142
+ .ProseMirror-menu-disabled.ProseMirror-icon {
143
+ cursor: default;
144
+ }
145
+
140
146
  .ProseMirror-icon svg {
141
147
  fill: currentColor;
142
148
  height: 1em;