aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/ProsemirrorExampleTransfer.ts
diff options
context:
space:
mode:
authormonikahedman <monika_hedman@brown.edu>2019-08-27 10:10:21 -0400
committermonikahedman <monika_hedman@brown.edu>2019-08-27 10:10:21 -0400
commitac4a5af7ec796c914509a696db0dbc749e0c40d3 (patch)
tree6bfc7b89abcf8ecc6cfac57011d3d942be646e1d /src/client/util/ProsemirrorExampleTransfer.ts
parent045d889f138861c5b22ec671e24aa553eaa4f441 (diff)
parent094bbd52f2b55f501357a9f6b057042ad7684f27 (diff)
Merge branch 'master' of https://github.com/browngraphicslab/Dash-Web into monika_linking
Diffstat (limited to 'src/client/util/ProsemirrorExampleTransfer.ts')
-rw-r--r--src/client/util/ProsemirrorExampleTransfer.ts70
1 files changed, 55 insertions, 15 deletions
diff --git a/src/client/util/ProsemirrorExampleTransfer.ts b/src/client/util/ProsemirrorExampleTransfer.ts
index b928532d6..3cdfba59a 100644
--- a/src/client/util/ProsemirrorExampleTransfer.ts
+++ b/src/client/util/ProsemirrorExampleTransfer.ts
@@ -2,8 +2,8 @@ import { chainCommands, exitCode, joinDown, joinUp, lift, selectParentNode, setB
import { redo, undo } from "prosemirror-history";
import { undoInputRule } from "prosemirror-inputrules";
import { Schema } from "prosemirror-model";
-import { liftListItem, splitListItem, wrapInList } from "prosemirror-schema-list";
-import { EditorState, Transaction } from "prosemirror-state";
+import { liftListItem, splitListItem, wrapInList, sinkListItem } from "prosemirror-schema-list";
+import { EditorState, Transaction, TextSelection, NodeSelection } from "prosemirror-state";
import { TooltipTextMenu } from "./TooltipTextMenu";
const mac = typeof navigator !== "undefined" ? /Mac/.test(navigator.platform) : false;
@@ -79,32 +79,72 @@ export default function buildKeymap<S extends Schema<any>>(schema: S, mapKeys?:
bind("Mod-s", TooltipTextMenu.insertStar);
- bind("Shift-Tab", (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => {
- var marks = state.storedMarks || (state.selection.$to.parentOffset && state.selection.$from.marks());
- liftListItem(schema.nodes.list_item)(state, (tx2: Transaction) => {
- marks && tx2.ensureMarks(marks);
- marks && tx2.setStoredMarks(marks);
- dispatch(tx2);
- });
- });
+ let levelMark = (depth: number) => {
+ let p10 = schema.marks.pFontSize.create({ fontSize: 10 });
+ let p14 = schema.marks.pFontSize.create({ fontSize: 14 });
+ let p18 = schema.marks.pFontSize.create({ fontSize: 18 });
+ let p24 = schema.marks.pFontSize.create({ fontSize: 24 });
+ return depth == 0 ? p24 : depth == 2 ? p18 : depth == 4 ? p14 : p10;
+ }
let bulletFunc = (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => {
var ref = state.selection;
var range = ref.$from.blockRange(ref.$to);
var marks = state.storedMarks || (state.selection.$to.parentOffset && state.selection.$from.marks());
let depth = range && range.depth ? range.depth : 0;
let nodeType = depth == 2 ? schema.nodes.cap_alphabet_list : depth == 4 ? schema.nodes.roman_list : depth == 6 ? schema.nodes.alphabet_list : schema.nodes.ordered_list;
+ let nodeTypeMark = depth == 2 ? schema.marks.mcap_alphabet_list : depth == 4 ? schema.marks.mroman_list : depth == 6 ? schema.marks.malphabet_list : schema.marks.mordered_list;
+ let created = levelMark(depth);
+ if (!sinkListItem(nodeType /*schema.nodes.list_item */)(state, (tx2: Transaction) => {
+ const resolvedPos = tx2.doc.resolve(range!.start);
+
+ let ns = new NodeSelection(resolvedPos);
+ let tx3 = tx2.addMark(ns.from - 1, ns.to, created).addMark(ns.from - 1, ns.to, nodeTypeMark as any).setSelection(TextSelection.create(tx2.doc, ns.to - (depth == 0 ? 3 : 1)));
+ marks && tx3.ensureMarks([...marks, created]);
+ marks && tx3.setStoredMarks([...marks, created]);
- if (!wrapInList(nodeType)(state, (tx2: Transaction) => {
- marks && tx2.ensureMarks(marks);
- marks && tx2.setStoredMarks(marks);
- dispatch(tx2);
+ dispatch(tx3);
})) {
- console.log("bullet fail");
+ let sxf = state.tr.setSelection(TextSelection.create(state.doc, range!.start, range!.end));
+ let newstate = state.applyTransaction(sxf);
+ if (!wrapInList(nodeType)(newstate.state, (tx2: Transaction) => {
+ const resolvedPos = tx2.doc.resolve(range!.start);
+ let ns = new TextSelection(resolvedPos, tx2.doc.resolve(range!.end + 1)); // new NodeSelection(resolvedPos);
+ let tx3 = tx2.setSelection(ns).removeMark(ns.from, ns.to, created).addMark(ns.from, ns.to, created).setSelection(TextSelection.create(tx2.doc, ns.to));
+ let tx4 = depth > 0 ? tx3.insertText(" ").setSelection(TextSelection.create(tx2.doc, ns.to - 2, ns.to + 2)).deleteSelection() : tx3;
+ marks && tx4.ensureMarks([...marks, created]);
+ marks && tx4.setStoredMarks([...marks, created]);
+
+ dispatch(tx4);
+ })) {
+ console.log("bullet fail");
+ }
}
}
bind("Tab", (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => bulletFunc(state, dispatch));
+ bind("Shift-Tab", (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => {
+ var ref = state.selection;
+ var range = ref.$from.blockRange(ref.$to);
+ var marks = state.storedMarks || (state.selection.$to.parentOffset && state.selection.$from.marks());
+ let created = levelMark(range && range.depth ? range.depth - 4 : 0);
+ liftListItem(schema.nodes.list_item)(state, (tx2: Transaction) => {
+ try {
+ const resolvedPos = tx2.doc.resolve(Math.round((range!.start + range!.end) / 2));
+ let nodeIndex = resolvedPos.pos - (resolvedPos.nodeBefore && resolvedPos.nodeBefore.type.name === "text" ? resolvedPos.nodeBefore!.nodeSize : 0);
+ let ns = new NodeSelection(tx2.doc.resolve(nodeIndex));
+ if (resolvedPos.nodeAfter && resolvedPos.nodeAfter.type.name === "list_item")
+ ns = new NodeSelection(tx2.doc.resolve(nodeIndex + 1));
+ let tx3 = tx2.setSelection(ns).removeMark(ns.from, ns.to, created).addMark(ns.from, ns.to, created).setSelection(TextSelection.create(tx2.doc, ns.to));
+ marks && tx3.ensureMarks([...marks, created]);
+ marks && tx3.setStoredMarks([...marks, created]);
+ dispatch(tx3);
+ } catch (e) {
+ dispatch(tx2);
+ }
+ });
+ });
+
bind("Enter", (state: EditorState<S>, dispatch: (tx: Transaction<S>) => void) => {
var marks = state.storedMarks || (state.selection.$to.parentOffset && state.selection.$from.marks());
if (!splitListItem(schema.nodes.list_item)(state, (tx3: Transaction) => {