Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const enhancedLinkHandler = ({ state, url, from, to, tr, type }: EnhancedLinkHandlerProps) => {
const endPosition = state.selection.to;
const enhancedLink = type.create({ href: extractHref(url) });
tr = (tr ?? state.tr).replaceWith(from, to, state.schema.text(url, [enhancedLink]));
// Ensure that the selection doesn't jump when the the current selection is within the range
if (endPosition < to) {
return tr.setSelection(TextSelection.create(tr.doc, endPosition));
}
return tr;
};
it("can go back and forth through history when preserving items", () => {
let state = mkState()
state = type(state, "one")
state = type(state, " two")
state = state.apply(closeHistory(state.tr))
state = state.apply(state.tr.insertText("xxx", state.selection.head).setMeta("addToHistory", false))
state = type(state, " three")
state = state.apply(state.tr.insertText("zero ", 1))
state = state.apply(closeHistory(state.tr))
state = state.apply(state.tr.split(1))
state = state.apply(state.tr.setSelection(TextSelection.create(state.doc, 1)))
state = type(state, "top")
state = state.apply(state.tr.insertText("yyy", 1).setMeta("addToHistory", false))
for (let i = 0; i < 3; i++) {
if (i == 2) compress(state)
for (let j = 0; j < 4; j++) state = command(state, undo)
ist(state.doc, doc(p("yyyxxx")), eq)
for (let j = 0; j < 4; j++) state = command(state, redo)
ist(state.doc, doc(p("yyytop"), p("zero one twoxxx three")), eq)
}
})
function defaultTripleClick(view, inside) {
let doc = view.state.doc
if (inside == -1) {
if (doc.inlineContent) {
updateSelection(view, TextSelection.create(doc, 0, doc.content.size), "pointer")
return true
}
return false
}
let $pos = doc.resolve(inside)
for (let i = $pos.depth + 1; i > 0; i--) {
let node = i > $pos.depth ? $pos.nodeAfter : $pos.node(i)
let nodePos = $pos.before(i)
if (node.inlineContent)
updateSelection(view, TextSelection.create(doc, nodePos + 1, nodePos + 1 + node.content.size), "pointer")
else if (NodeSelection.isSelectable(node))
updateSelection(view, NodeSelection.create(doc, nodePos), "pointer")
else
continue
return true
}
}
export const dispatchTextSelection = ({ view, start, end }: DispatchTextSelectionParams) => {
const { state } = view;
const tr = state.tr.setSelection(TextSelection.create(state.doc, start, end));
view.dispatch(tr);
};
asProseMirrorSelection(doc: Node): Selection {
const offset = this.getPos() + 1
const anchor = this.cm.indexFromPos(this.cm.getCursor('anchor')) + offset
const head = this.cm.indexFromPos(this.cm.getCursor('head')) + offset
return TextSelection.create(doc, anchor, head)
}
return tr;
}
const body = schema.nodes['body'];
if (!body) {
return tr;
}
const {doc} = tr;
if (!doc || doc.nodeSize < 2) {
return tr;
}
const node = doc.nodeAt(0);
if (!node || node.type !== body || node.nodeSize <= 4) {
return tr;
}
tr = tr.setSelection(TextSelection.create(
doc,
2,
node.nodeSize - 2,
));
return tr;
}
const getChar = (start: number, end: number) =>
getTextContentFromSlice(TextSelection.create(state.doc, start, end).content());
return new InputRule(SmileyConf.getRegex(), ((state, match) => {
const { tr } = state;
const syntax = match[1];
const icon = SmileyConf.getSmileys()[syntax];
tr.setSelection(TextSelection.create(tr.doc, tr.selection.from, tr.selection.from - syntax.length + 1));
return tr.replaceSelectionWith(state.schema.nodes.smiley.create({ icon, syntax }));
}));
}
setFocus() {
let tr = this.view.state.tr;
const textSelection = TextSelection.create(tr.doc, 10, 16);
tr = tr.setSelection(textSelection).scrollIntoView();
this.view.dispatch(tr);
this.view.focus();
}