-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e159696
commit f6b52e0
Showing
14 changed files
with
240 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type { Command } from "prosemirror-state"; | ||
|
||
import { EditorState, NodeSelection } from "prosemirror-state"; | ||
|
||
import type { Dispatch } from "./types"; | ||
import { createCommandSpec } from "./util"; | ||
|
||
const toggleKind: Command = (state: EditorState, dispatch?: Dispatch) => { | ||
const { node } = state.selection as NodeSelection; | ||
const canRun = node && (node.type.name === "math_inline" || node.type.name === "math_display"); | ||
if (!canRun) { | ||
return false; | ||
} | ||
const isDisplay = node.type.name === "math_display"; | ||
if (dispatch) { | ||
const { | ||
schema: { | ||
nodes: { math_display: displayType, math_inline: inlineType }, | ||
}, | ||
} = state; | ||
const swapNodeType = isDisplay ? inlineType : displayType; | ||
const transaction = state.tr.replaceSelectionWith( | ||
swapNodeType.create({}, node.content), | ||
true | ||
); | ||
dispatch(transaction); | ||
} | ||
return true; | ||
}; | ||
|
||
const toggleLabel: Command = (state: EditorState, dispatch?: Dispatch) => { | ||
const { node, $anchor } = state.selection as NodeSelection; | ||
const canRun = node && node.type.name === "math_display"; | ||
if (!canRun) return false; | ||
if (dispatch) { | ||
const transaction = state.tr.setNodeMarkup( | ||
$anchor.pos, | ||
node.type, | ||
{ | ||
hideLabel: !node.attrs.hideLabel, | ||
}, | ||
node.marks | ||
); | ||
dispatch(transaction); | ||
} | ||
return true; | ||
}; | ||
|
||
export const mathToggleKind = createCommandSpec((dispatch, state) => ({ | ||
run: () => toggleKind(state, dispatch), | ||
canRun: toggleKind(state), | ||
isActive: false, | ||
})); | ||
export const mathToggleLabel = createCommandSpec((dispatch, state) => ({ | ||
run: () => toggleLabel(state, dispatch), | ||
canRun: toggleLabel(state), | ||
isActive: false, | ||
})); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { DOMOutputSpec, MarkSpec, NodeSpec } from "prosemirror-model"; | ||
|
||
const mathInline = { | ||
attrs: { | ||
id: { default: null }, | ||
class: { default: null }, | ||
}, | ||
content: "text*", | ||
group: "inline math", | ||
inline: true, | ||
atom: true, | ||
parseDOM: [ | ||
{ | ||
tag: "math-inline", | ||
getAttrs: (node) => { | ||
return { | ||
id: (node as Element).getAttribute("id"), | ||
class: (node as Element).getAttribute("class"), | ||
}; | ||
}, | ||
}, | ||
], | ||
toDOM: () => ["math-inline", { class: "math-node" }, 0], | ||
} satisfies NodeSpec; | ||
|
||
const mathDisplay = { | ||
attrs: { | ||
id: { default: null }, | ||
class: { default: null }, | ||
}, | ||
group: "block math", | ||
content: "text*", | ||
atom: true, | ||
code: true, | ||
parseDOM: [ | ||
{ | ||
tag: "math-display", | ||
getAttrs: (node) => { | ||
return { | ||
id: (node as Element).getAttribute("id"), | ||
class: (node as Element).getAttribute("class"), | ||
}; | ||
}, | ||
}, | ||
], | ||
toDOM: () => ["math-display", { class: "math-node" }, 0], | ||
} satisfies NodeSpec; | ||
|
||
export default { | ||
math_inline: mathInline, | ||
math_display: mathDisplay, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { EditorView } from "prosemirror-view"; | ||
|
||
import { Node } from "prosemirror-model"; | ||
|
||
export const insertNodeIntoEditor = (view: EditorView, nodeType: string, attrs?: Node["attrs"]) => { | ||
const { schema, tr } = view.state; | ||
const nodeSchema = schema.nodes[nodeType]; | ||
if (nodeSchema.spec.onInsert) { | ||
nodeSchema.spec.onInsert(view, attrs); | ||
} else { | ||
const node = nodeSchema.create(attrs); | ||
const transaction = tr.replaceSelectionWith(node); | ||
view.dispatch(transaction); | ||
} | ||
}; |
Oops, something went wrong.