Skip to content

Commit

Permalink
ignore-space-improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed Apr 4, 2023
1 parent 896fe23 commit 9961b93
Showing 1 changed file with 56 additions and 46 deletions.
102 changes: 56 additions & 46 deletions asset/js/widget/TermInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {

this.separator = this.input.dataset.termSeparator || ' ';
this.ignoreSpaceUntil = null;
this.ignoreSpaceSince = null;
}

bind() {
Expand All @@ -23,36 +22,54 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
super.reset();

this.ignoreSpaceUntil = null;
this.ignoreSpaceSince = null;
}

readPartialTerm(input) {
let value = super.readPartialTerm(input);
if (value && this.ignoreSpaceUntil && value[0] === this.ignoreSpaceUntil) {
value = value.slice(1);
if (value.slice(-1) === this.ignoreSpaceUntil) {
value = value.slice(0, -1);
}
}

return value;
}

readFullTerm(input, termIndex = null) {
let termData = super.readFullTerm(input, termIndex);
if (this.ignoreSpaceUntil !== null && termData.label[this.ignoreSpaceSince] === this.ignoreSpaceUntil) {
if (termData.label.length - 1 === this.ignoreSpaceSince
|| termData.label.slice(-1) !== this.ignoreSpaceUntil
|| (this.ignoreSpaceSince === 0 && (termData.label.length < 2
|| termData.label.slice(0, 1) !== this.ignoreSpaceUntil)
)
) {
if (termData && this.ignoreSpaceUntil !== null && input.value[0] === this.ignoreSpaceUntil) {
if (input.value.slice(-1) !== this.ignoreSpaceUntil || input.value.length < 2) {
return false;
}

this.ignoreSpaceUntil = null;
}

return termData;
}

addTerm(termData, termIndex = null) {
if (this.ignoreSpaceUntil !== null) {
if (this.ignoreSpaceSince === 0 && termData.label[this.ignoreSpaceSince] === this.ignoreSpaceUntil) {
termData.label = termData.label.slice(1, -1);
hasSyntaxError(input) {
if (input === this.input && this.ignoreSpaceUntil !== null) {
if (input.value === this.ignoreSpaceUntil) {
return true;
}
}

this.ignoreSpaceUntil = null;
this.ignoreSpaceSince = null;
return super.hasSyntaxError(input);
}

termsToQueryString(terms) {
let quoted = [];
for (const termData of terms) {
if (termData.search.indexOf(this.separator) >= 0) {
quoted.push({ ...termData, search: '"' + termData.search + '"' });
} else {
quoted.push(termData);
}
}

super.addTerm(termData, termIndex);
return super.termsToQueryString(quoted);
}

complete(input, data) {
Expand Down Expand Up @@ -95,7 +112,29 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
super.onSubmit(event);

this.ignoreSpaceUntil = null;
this.ignoreSpaceSince = null;
}

onInput(event) {
let label = event.target.parentNode;
if (label.dataset.index >= 0) {
return;
}

let input = event.target;
let firstChar = input.value[0];

if (this.ignoreSpaceUntil !== null) {
// Reset if the user changes/removes the source char
if (firstChar !== this.ignoreSpaceUntil) {
this.ignoreSpaceUntil = null;
}
}

if (this.ignoreSpaceUntil === null && (firstChar === "'" || firstChar === '"')) {
this.ignoreSpaceUntil = firstChar;
}

super.onInput(event);
}

onKeyDown(event) {
Expand All @@ -121,34 +160,6 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
}
}

onKeyUp(event) {
super.onKeyUp(event);

let label = event.target.parentNode;
if (label.dataset.index >= 0) {
return;
}

if (this.ignoreSpaceUntil !== null) {
// Reset if the user changes/removes the source char
let value = event.target.value;
if (value[this.ignoreSpaceSince] !== this.ignoreSpaceUntil) {
this.ignoreSpaceUntil = null;
this.ignoreSpaceSince = null;
}
}

let input = event.target;
let firstChar = this.readPartialTerm(input).charAt(0);

if (firstChar === "'" || firstChar === '"') {
if (this.ignoreSpaceUntil === null) {
this.ignoreSpaceUntil = firstChar;
this.ignoreSpaceSince = input.selectionStart - 1;
}
}
}

onTermClick(event) {
let termIndex = event.target.parentNode.dataset.index;
this.removeTerm(event.target.parentNode);
Expand All @@ -167,7 +178,6 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
event.preventDefault();

this.ignoreSpaceUntil = null;
this.ignoreSpaceSince = null;
}
}
}
Expand Down

0 comments on commit 9961b93

Please sign in to comment.