Skip to content

Commit

Permalink
Correctly create chipWrapper nodes for partial tags that precede exis…
Browse files Browse the repository at this point in the history
…ting tags
  • Loading branch information
jonathonherbert committed Sep 25, 2024
1 parent ca6f847 commit 6e8b3d6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
17 changes: 16 additions & 1 deletion prosemirror-client/src/cqlInput/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe("utils", () => {
};

describe("tokensToNode", () => {
test("creates nodes from a list of tokens - 1", async () => {
test("creates nodes from a list of tokens", async () => {
const tokens = await queryToProseMirrorTokens("text +key:value text");
const node = tokensToDoc(tokens);

Expand Down Expand Up @@ -78,6 +78,21 @@ describe("utils", () => {

expect(node.toJSON()).toEqual(expected.toJSON());
});

test("should create chipWrappers for partial tags that precede existing tags", async () => {
const tokens = await queryToProseMirrorTokens("+ +tag");
const node = tokensToDoc(tokens);

const expected = doc(
searchText(),
chipWrapper(chip(chipKey(), chipValue())),
searchText(),
chipWrapper(chip(chipKey("tag"), chipValue())),
searchText()
);

expect(node.toJSON()).toEqual(expected.toJSON());
});
});

describe("mapTokens", () => {
Expand Down
8 changes: 6 additions & 2 deletions prosemirror-client/src/cqlInput/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,13 @@ export const tokensToDoc = (_tokens: ProseMirrorToken[]): Node => {
switch (token.tokenType) {
case "QUERY_FIELD_KEY": {
const tokenKey = token.literal;
const tokenValue = tokens[index + 1]?.literal;
const nextToken = tokens[index + 1];
const tokenValue =
nextToken.tokenType === "QUERY_VALUE" ? nextToken.literal : "";
const previousToken = tokens[index - 1];
const isPrecededByChip = previousToken?.tokenType === "QUERY_VALUE";
const isPrecededByChip =
previousToken?.tokenType === "QUERY_VALUE" ||
previousToken?.tokenType === "QUERY_FIELD_KEY";
return acc.concat(
...(isPrecededByChip ? [searchText.create()] : []),
chipWrapper.create(undefined, [
Expand Down

0 comments on commit 6e8b3d6

Please sign in to comment.