diff --git a/wax-prosemirror-components/src/mainMenuBar/MainMenuBarItems.js b/wax-prosemirror-components/src/mainMenuBar/MainMenuBarItems.js index 6ae021c2b9d9765421cde310bf50ea5d9fb583a0..2497edb3a7a8dc6766d220f724774d967f2216e4 100644 --- a/wax-prosemirror-components/src/mainMenuBar/MainMenuBarItems.js +++ b/wax-prosemirror-components/src/mainMenuBar/MainMenuBarItems.js @@ -21,47 +21,13 @@ import TableDropDown from "../components/TableDropDown"; import ImageUpload from "../components/ImageUpload"; import HeadingsDropDown from "../components/HeadingsDropDown"; -const markActive = type => state => { - const { from, $from, to, empty } = state.selection; - - return empty - ? type.isInSet(state.storedMarks || $from.marks()) - : state.doc.rangeHasMark(from, to, type); -}; - -const blockActive = (type, attrs = {}) => state => { - const { $from, to, node } = state.selection; - - if (node) { - return node.hasMarkup(type, attrs); - } - - return to <= $from.end() && $from.parent.hasMarkup(type, attrs); -}; - -const canInsert = type => state => { - const { $from } = state.selection; - - for (let d = $from.depth; d >= 0; d--) { - const index = $from.index(d); - - if ($from.node(d).canReplaceWith(index, index, type)) { - return true; - } - } - - return false; -}; - -const promptForURL = () => { - let url = window && window.prompt("Enter the URL", "https://"); - - if (url && !/^https?:\/\//i.test(url)) { - url = "http://" + url; - } - - return url; -}; +import { + markActive, + blockActive, + canInsert, + promptForURL, + createTable +} from "./MainMenuCommands"; export default { undo: { @@ -279,23 +245,7 @@ export default { return canInsert(state.config.schema.nodes.table)(state); }, run: (state, dispatch) => { - let rowCount = window && window.prompt("How many rows?", 2); - let colCount = window && window.prompt("How many columns?", 2); - - const cells = []; - while (colCount--) { - cells.push(state.config.schema.nodes.table_cell.createAndFill()); - } - - const rows = []; - while (rowCount--) { - rows.push( - state.config.schema.nodes.table_row.createAndFill(null, cells) - ); - } - - const table = state.config.schema.nodes.table.createAndFill(null, rows); - dispatch(state.tr.replaceSelectionWith(table)); + return createTable(state, dispatch); }, select: state => true, menu: props => <Button key={uuid()} {...props} /> diff --git a/wax-prosemirror-components/src/mainMenuBar/MainMenuCommands.js b/wax-prosemirror-components/src/mainMenuBar/MainMenuCommands.js new file mode 100644 index 0000000000000000000000000000000000000000..ab7f1da0a7d2d652672ef4f784aa6587ad3a39d2 --- /dev/null +++ b/wax-prosemirror-components/src/mainMenuBar/MainMenuCommands.js @@ -0,0 +1,61 @@ +const markActive = type => state => { + const { from, $from, to, empty } = state.selection; + + return empty + ? type.isInSet(state.storedMarks || $from.marks()) + : state.doc.rangeHasMark(from, to, type); +}; + +const blockActive = (type, attrs = {}) => state => { + const { $from, to, node } = state.selection; + + if (node) { + return node.hasMarkup(type, attrs); + } + + return to <= $from.end() && $from.parent.hasMarkup(type, attrs); +}; + +const canInsert = type => state => { + const { $from } = state.selection; + + for (let d = $from.depth; d >= 0; d--) { + const index = $from.index(d); + + if ($from.node(d).canReplaceWith(index, index, type)) { + return true; + } + } + + return false; +}; + +const promptForURL = () => { + let url = window && window.prompt("Enter the URL", "https://"); + + if (url && !/^https?:\/\//i.test(url)) { + url = "http://" + url; + } + + return url; +}; + +const createTable = (state, dispatch) => { + let rowCount = window && window.prompt("How many rows?", 2); + let colCount = window && window.prompt("How many columns?", 2); + + const cells = []; + while (colCount--) { + cells.push(state.config.schema.nodes.table_cell.createAndFill()); + } + + const rows = []; + while (rowCount--) { + rows.push(state.config.schema.nodes.table_row.createAndFill(null, cells)); + } + + const table = state.config.schema.nodes.table.createAndFill(null, rows); + dispatch(state.tr.replaceSelectionWith(table)); +}; + +export { markActive, blockActive, canInsert, promptForURL, createTable }; diff --git a/wax-prosemirror-components/src/sideMenuBar/SideMenuCommands.js b/wax-prosemirror-components/src/sideMenuBar/SideMenuCommands.js new file mode 100644 index 0000000000000000000000000000000000000000..b9620b65822dcfcefe5e41c41de9e80e136bf852 --- /dev/null +++ b/wax-prosemirror-components/src/sideMenuBar/SideMenuCommands.js @@ -0,0 +1,10 @@ +const blockActive = (type, attrs = {}) => state => { + const { $from, to, node } = state.selection; + + if (node) { + return node.hasMarkup(type, attrs); + } + return to <= $from.end() && $from.parent.hasMarkup(type, attrs); +}; + +export { blockActive }; diff --git a/wax-prosemirror-components/src/sideMenuBar/SideMenuItems.js b/wax-prosemirror-components/src/sideMenuBar/SideMenuItems.js index c580085f678386b64187526ce8a01c9e48e86542..a92e8e78654100828b081d36c3438b7a6a4d5897 100644 --- a/wax-prosemirror-components/src/sideMenuBar/SideMenuItems.js +++ b/wax-prosemirror-components/src/sideMenuBar/SideMenuItems.js @@ -3,16 +3,7 @@ import { v4 as uuid } from "uuid"; import { setBlockType } from "prosemirror-commands"; import Button from "../components/button/Button"; - -const blockActive = (type, attrs = {}) => state => { - const { $from, to, node } = state.selection; - - if (node) { - return node.hasMarkup(type, attrs); - } - return to <= $from.end() && $from.parent.hasMarkup(type, attrs); -}; - +import { blockActive } from "./SideMenuItems"; export default { title: { title: "Change to Title",