Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Commit

Permalink
Flags profile conditions (#1647)
Browse files Browse the repository at this point in the history
* Generalize modifier keys

* Optimize bindings

* Add support for flags

* Add clipboard flag

* Update tests

* Add tests
  • Loading branch information
toasted-nutbread authored May 1, 2021
1 parent 8bf6ff9 commit c514bbc
Show file tree
Hide file tree
Showing 4 changed files with 366 additions and 31 deletions.
67 changes: 52 additions & 15 deletions ext/js/background/profile-conditions-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ class ProfileConditionsUtil {
['notInclude', this._createSchemaModifierKeysNotInclude.bind(this)]
])
}
],
[
'flags',
{
operators: new Map([
['are', this._createSchemaFlagsAre.bind(this)],
['areNot', this._createSchemaFlagsAreNot.bind(this)],
['include', this._createSchemaFlagsInclude.bind(this)],
['notInclude', this._createSchemaFlagsNotInclude.bind(this)]
])
}
]
]);
}
Expand Down Expand Up @@ -121,6 +132,10 @@ class ProfileConditionsUtil {
// NOP
}
}
const {flags} = normalizedContext;
if (!Array.isArray(flags)) {
normalizedContext.flags = [];
}
return normalizedContext;
}

Expand Down Expand Up @@ -222,54 +237,76 @@ class ProfileConditionsUtil {
// modifierKeys schema creation functions

_createSchemaModifierKeysAre(value) {
return this._createSchemaModifierKeysGeneric(value, true, false);
return this._createSchemaArrayCheck('modifierKeys', value, true, false);
}

_createSchemaModifierKeysAreNot(value) {
return {
not: [this._createSchemaModifierKeysGeneric(value, true, false)]
not: [this._createSchemaArrayCheck('modifierKeys', value, true, false)]
};
}

_createSchemaModifierKeysInclude(value) {
return this._createSchemaModifierKeysGeneric(value, false, false);
return this._createSchemaArrayCheck('modifierKeys', value, false, false);
}

_createSchemaModifierKeysNotInclude(value) {
return this._createSchemaModifierKeysGeneric(value, false, true);
return this._createSchemaArrayCheck('modifierKeys', value, false, true);
}

// modifierKeys schema creation functions

_createSchemaFlagsAre(value) {
return this._createSchemaArrayCheck('flags', value, true, false);
}

_createSchemaFlagsAreNot(value) {
return {
not: [this._createSchemaArrayCheck('flags', value, true, false)]
};
}

_createSchemaModifierKeysGeneric(value, exact, none) {
_createSchemaFlagsInclude(value) {
return this._createSchemaArrayCheck('flags', value, false, false);
}

_createSchemaFlagsNotInclude(value) {
return this._createSchemaArrayCheck('flags', value, false, true);
}

// Generic

_createSchemaArrayCheck(key, value, exact, none) {
const containsList = [];
for (const modifierKey of this._split(value)) {
if (modifierKey.length === 0) { continue; }
for (const item of this._split(value)) {
if (item.length === 0) { continue; }
containsList.push({
contains: {
const: modifierKey
const: item
}
});
}
const containsListCount = containsList.length;
const modifierKeysSchema = {
const schema = {
type: 'array'
};
if (exact) {
modifierKeysSchema.maxItems = containsListCount;
schema.maxItems = containsListCount;
}
if (none) {
if (containsListCount > 0) {
modifierKeysSchema.not = containsList;
schema.not = containsList;
}
} else {
modifierKeysSchema.minItems = containsListCount;
schema.minItems = containsListCount;
if (containsListCount > 0) {
modifierKeysSchema.allOf = containsList;
schema.allOf = containsList;
}
}
return {
required: ['modifierKeys'],
required: [key],
properties: {
modifierKeys: modifierKeysSchema
[key]: schema
}
};
}
Expand Down
14 changes: 9 additions & 5 deletions ext/js/display/search-display-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ class SearchDisplayController {
e.preventDefault();
e.stopImmediatePropagation();
this._display.blurElement(e.currentTarget);
this._search(true, true, true);
this._search(true, true, true, null);
}

_onSearch(e) {
e.preventDefault();
this._search(true, true, true);
this._search(true, true, true, null);
}

_onCopy() {
Expand All @@ -199,7 +199,7 @@ class SearchDisplayController {
}
this._queryInput.value = text;
this._updateSearchHeight(true);
this._search(animate, false, autoSearchContent);
this._search(animate, false, autoSearchContent, ['clipboard']);
}

_onWanakanaEnableChange(e) {
Expand Down Expand Up @@ -342,11 +342,15 @@ class SearchDisplayController {
});
}

_search(animate, history, lookup) {
_search(animate, history, lookup, flags) {
const query = this._queryInput.value;
const depth = this._display.depth;
const url = window.location.href;
const documentTitle = document.title;
const optionsContext = {depth, url};
if (flags !== null) {
optionsContext.flags = flags;
}
const details = {
focus: false,
history,
Expand All @@ -355,7 +359,7 @@ class SearchDisplayController {
},
state: {
focusEntry: 0,
optionsContext: {depth, url},
optionsContext,
url,
sentence: {text: query, offset: 0},
documentTitle
Expand Down
46 changes: 40 additions & 6 deletions ext/js/pages/settings/profile-conditions-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,23 @@ class ProfileConditionsUI extends EventDispatcher {
this._eventListeners = new EventListenerCollection();
this._defaultType = 'popupLevel';
this._profileIndex = 0;
const validateInteger = this._validateInteger.bind(this);
const normalizeInteger = this._normalizeInteger.bind(this);
const validateFlags = this._validateFlags.bind(this);
const normalizeFlags = this._normalizeFlags.bind(this);
this._descriptors = new Map([
[
'popupLevel',
{
displayName: 'Popup Level',
defaultOperator: 'equal',
operators: new Map([
['equal', {displayName: '=', type: 'integer', defaultValue: '0', validate: this._validateInteger.bind(this), normalize: this._normalizeInteger.bind(this)}],
['notEqual', {displayName: '\u2260', type: 'integer', defaultValue: '0', validate: this._validateInteger.bind(this), normalize: this._normalizeInteger.bind(this)}],
['lessThan', {displayName: '<', type: 'integer', defaultValue: '0', validate: this._validateInteger.bind(this), normalize: this._normalizeInteger.bind(this)}],
['greaterThan', {displayName: '>', type: 'integer', defaultValue: '0', validate: this._validateInteger.bind(this), normalize: this._normalizeInteger.bind(this)}],
['lessThanOrEqual', {displayName: '\u2264', type: 'integer', defaultValue: '0', validate: this._validateInteger.bind(this), normalize: this._normalizeInteger.bind(this)}],
['greaterThanOrEqual', {displayName: '\u2265', type: 'integer', defaultValue: '0', validate: this._validateInteger.bind(this), normalize: this._normalizeInteger.bind(this)}]
['equal', {displayName: '=', type: 'integer', defaultValue: '0', validate: validateInteger, normalize: normalizeInteger}],
['notEqual', {displayName: '\u2260', type: 'integer', defaultValue: '0', validate: validateInteger, normalize: normalizeInteger}],
['lessThan', {displayName: '<', type: 'integer', defaultValue: '0', validate: validateInteger, normalize: normalizeInteger}],
['greaterThan', {displayName: '>', type: 'integer', defaultValue: '0', validate: validateInteger, normalize: normalizeInteger}],
['lessThanOrEqual', {displayName: '\u2264', type: 'integer', defaultValue: '0', validate: validateInteger, normalize: normalizeInteger}],
['greaterThanOrEqual', {displayName: '\u2265', type: 'integer', defaultValue: '0', validate: validateInteger, normalize: normalizeInteger}]
])
}
],
Expand All @@ -69,8 +73,24 @@ class ProfileConditionsUI extends EventDispatcher {
['notInclude', {displayName: 'Don\'t Include', type: 'modifierKeys', defaultValue: ''}]
])
}
],
[
'flags',
{
displayName: 'Flags',
defaultOperator: 'are',
operators: new Map([
['are', {displayName: 'Are', type: 'string', defaultValue: '', validate: validateFlags, normalize: normalizeFlags}],
['areNot', {displayName: 'Are Not', type: 'string', defaultValue: '', validate: validateFlags, normalize: normalizeFlags}],
['include', {displayName: 'Include', type: 'string', defaultValue: '', validate: validateFlags, normalize: normalizeFlags}],
['notInclude', {displayName: 'Don\'t Include', type: 'string', defaultValue: '', validate: validateFlags, normalize: normalizeFlags}]
])
}
]
]);
this._validFlags = new Set([
'clipboard'
]);
}

get settingsController() {
Expand Down Expand Up @@ -280,6 +300,20 @@ class ProfileConditionsUI extends EventDispatcher {
return this.splitValue(value).join(', ');
}

_validateFlags(value) {
const flags = this.splitValue(value);
for (const flag of flags) {
if (!this._validFlags.has(flag)) {
return false;
}
}
return flags.length > 0;
}

_normalizeFlags(value) {
return [...new Set(this.splitValue(value))].join(', ');
}

_triggerConditionGroupCountChanged(count) {
this.trigger('conditionGroupCountChanged', {count, profileIndex: this._profileIndex});
}
Expand Down
Loading

0 comments on commit c514bbc

Please sign in to comment.