Skip to content

Commit

Permalink
Use selection relative to chip, not diff, to determine whether to mov…
Browse files Browse the repository at this point in the history
…e selection on parse
  • Loading branch information
jonathonherbert committed Nov 15, 2024
1 parent a8a78bf commit caec68b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 45 deletions.
8 changes: 8 additions & 0 deletions prosemirror-client/src/cqlInput/editor/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,14 @@ describe("plugin", () => {

await waitFor("a +tag: b");
});

it("permits additional query fields before query fields", async () => {
const { editor, waitFor } = createCqlEditor("+2");

await editor.shortcut("Ctrl-a").insertText("+1");

await waitFor("+1: +2: ");
});
});

describe("deletion", () => {
Expand Down
11 changes: 2 additions & 9 deletions prosemirror-client/src/cqlInput/editor/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,7 @@ export const createCqlPlugin = ({
*
* Side-effects: mutates the given transaction, and re-applies debug UI if provided.
*/
const applyQueryToTr = (
tr: Transaction,
cqlService: CqlServiceInterface,
prevQuery?: string
) => {
const applyQueryToTr = (tr: Transaction, cqlService: CqlServiceInterface) => {
const queryBeforeParse = docToQueryStr(tr.doc);

const result = cqlService.parseCqlQueryStr(queryBeforeParse);
Expand Down Expand Up @@ -161,8 +157,6 @@ export const createCqlPlugin = ({
getNewSelection({
selection: userSelection,
doc: tr.doc,
query: queryAfterParse,
prevQuery,
})
);
}
Expand Down Expand Up @@ -226,8 +220,7 @@ export const createCqlPlugin = ({
if (maybeQueries) {
const { queryResult, tr: newTr } = applyQueryToTr(
newState.tr,
cqlService,
maybeQueries.prevQuery
cqlService
);

tr = newTr;
Expand Down
40 changes: 4 additions & 36 deletions prosemirror-client/src/cqlInput/editor/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,37 +317,6 @@ export const docToQueryStr = (doc: Node) => {
return str;
};

const keyValPairChars = ["+"];

export const isBeginningKeyValPair = (
first: string | undefined,
second: string
): boolean => {
if (!first) {
return keyValPairChars.includes(second[0]);
}
const firstDiffChar = getFirstNonWhitespaceDiff(first, second);
return firstDiffChar ? keyValPairChars.includes(firstDiffChar) : false;
};

const getFirstNonWhitespaceDiff = (
_first: string,
_second: string
): string | undefined => {
const first = _first.replaceAll(" ", "");
const second = _second.replaceAll(" ", "");

if (first.length < second.length) {
return second[first.length];
}

for (let i = 0; i < first.length; i++) {
if (second[i] !== first[i]) {
return second[i];
}
}
};

export const findNodeAt = (pos: number, doc: Node, type: NodeType): number => {
let found = -1;
doc.nodesBetween(pos - 1, doc.content.size, (node, pos) => {
Expand All @@ -363,15 +332,14 @@ export const findNodeAt = (pos: number, doc: Node, type: NodeType): number => {
export const getNewSelection = ({
selection,
doc,
prevQuery,
query,
}: {
selection: Selection;
doc: Node;
prevQuery?: string;
query: string;
}): Selection => {
const shouldWrapSelectionInKey = isBeginningKeyValPair(prevQuery, query);
const shouldWrapSelectionInKey =
selection.from === selection.to &&
// Is the selection just before the start of a chip?
doc.resolve(selection.from).nodeAfter?.type === chip;

if (shouldWrapSelectionInKey) {
const nodePos = findNodeAt(selection.from, doc, chipKey);
Expand Down

0 comments on commit caec68b

Please sign in to comment.