From edda803babf947a9f9b53ba168e74c2e7976ce8e Mon Sep 17 00:00:00 2001 From: pedromml Date: Tue, 15 Oct 2024 05:55:34 -0300 Subject: [PATCH 1/2] Update source prerequisite behaviour --- modules/presets/index.js | 25 +++++++++++++++++++------ modules/ui/field.js | 4 ++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/modules/presets/index.js b/modules/presets/index.js index 7c4689faa9..e197595a99 100644 --- a/modules/presets/index.js +++ b/modules/presets/index.js @@ -63,7 +63,6 @@ function addHistoricalFields(fields) { for (let i = 1; i < 4; i++){ let id = 'source:' + i.toString(); - let previousId = 'source' + ((i-1) > 0 ? ':' + (i-1).toString() : ''); fields[id] = { ...fields.source, key: id, @@ -72,11 +71,25 @@ function addHistoricalFields(fields) { baseKey: 'source', index: i, prerequisiteTag: { - keys: [ - previousId, - previousId + ':url', - previousId + ':name', - previousId + ':date']}}; + prerequisiteFunction: (tag) => { + let sourceRegex = /^source/; + if (tag.match(sourceRegex) ){ + // allow index 1 if there are any source tags present + if (i === 1){ + return true; + } else { + // for index > 1, get the index in the tag with a regex + let indexRegex = /(?<=\:)\d+/; + let regexMatch = parseInt(tag.match(indexRegex), 10); + // allow index i if the index directly before it is present OR if any index above it is present + if (regexMatch === i-1 || regexMatch > i){ + return true; + } + } + } + return false; + }} + }; } } diff --git a/modules/ui/field.js b/modules/ui/field.js index c8e821a45f..caffa80cd4 100644 --- a/modules/ui/field.js +++ b/modules/ui/field.js @@ -354,6 +354,10 @@ export function uiField(context, presetField, entityIDs, options) { if (!entityIDs.every(function(entityID) { var entity = context.graph().entity(entityID); + if (prerequisiteTag.prerequisiteFunction) { + // Return true if prerequisiteFunction returns true for any tag + return Object.keys(entity.tags).some(prerequisiteTag.prerequisiteFunction); + } if (prerequisiteTag.keys) { // Return true if any key in prerequisiteTag.keys is present, return false otherwise // If prerequisiteTag.keys is present, prerequisiteTag.key will be ignored From f91a3dd38a9a662b1c8d2d984e5a6fccafcf314b Mon Sep 17 00:00:00 2001 From: pedromml Date: Wed, 6 Nov 2024 11:41:13 -0300 Subject: [PATCH 2/2] Replace regex match --- modules/presets/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/presets/index.js b/modules/presets/index.js index e197595a99..f5c714dbbc 100644 --- a/modules/presets/index.js +++ b/modules/presets/index.js @@ -72,8 +72,7 @@ function addHistoricalFields(fields) { index: i, prerequisiteTag: { prerequisiteFunction: (tag) => { - let sourceRegex = /^source/; - if (tag.match(sourceRegex) ){ + if (tag.startsWith('source') ){ // allow index 1 if there are any source tags present if (i === 1){ return true;