From 63e57d785dc28ea846bf22243be759177bf61ff1 Mon Sep 17 00:00:00 2001 From: Ross Keenan Date: Wed, 10 Nov 2021 09:40:02 +0200 Subject: [PATCH] fix(getFieldValues): :bug: Handle DataArrays (dv proxies) --- main.js | 77 ++++++++++++++++++++++++++++++++---------- package.json | 1 + src/sharedFunctions.ts | 77 +++++++++++++++++++++++++++++++----------- 3 files changed, 118 insertions(+), 37 deletions(-) diff --git a/main.js b/main.js index a2bad559..7d19becf 100644 --- a/main.js +++ b/main.js @@ -1,8 +1,13 @@ 'use strict'; var obsidian = require('obsidian'); +var util = require('util'); require('path'); +function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } + +var util__default = /*#__PURE__*/_interopDefaultLegacy(util); + var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; function createCommonjsModule(fn) { @@ -6399,25 +6404,63 @@ function getFieldValues(frontmatterCache, field, settings) { rawValues.forEach((rawItem) => { if (!rawItem) return; - if (typeof rawItem === "string" || typeof rawItem === "number") { - // Obs cache converts link of form: [[\d+]] to number[][] - const rawItemAsString = rawItem.toString(); - const splits = rawItemAsString.match(splitLinksRegex); - if (splits !== null) { - const strs = splits.map((link) => link.match(dropHeaderOrAlias)[1].split("/").last()); - values.push(...strs); - } - else { - values.push(rawItemAsString.split("/").last()); - } + let unProxied = [rawItem]; + if (util__default['default'].types.isProxy(rawItem)) { + unProxied = []; + // Definitely a proxy the first time + const firstValue = Object.assign({}, rawItem); + firstValue.values.forEach((firstVal) => { + if (util__default['default'].types.isProxy(firstVal)) { + const secondValue = Object.assign({}, firstVal); + const secondValues = secondValue.values; + if (secondValues) { + secondValues.forEach((secondVal) => { + if (util__default['default'].types.isProxy(secondVal)) { + const thirdValues = Object.assign({}, secondVal).values; + thirdValues.forEach((thirdVal) => { + unProxied.push(thirdVal); + }); + } + else { + if (typeof secondValues === "string") { + unProxied.push(secondValues); + } + else { + unProxied.push(...secondValues); + } + } + }); + } + else { + unProxied.push(secondValue); + } + } + else { + unProxied.push(firstVal); + } + }); } - else if (rawItem.path !== undefined) { - superDebug(settings, { rawItem }); - const lastSplit = rawItem.path.split("/").last(); - if (lastSplit !== undefined) { - values.push(lastSplit); + unProxied.forEach((value) => { + console.log({ unproxiedValue: value }); + if (typeof value === "string" || typeof value === "number") { + // Obs cache converts link of form: [[\d+]] to number[][] + const rawItemAsString = value.toString(); + const splits = rawItemAsString.match(splitLinksRegex); + if (splits !== null) { + const strs = splits.map((link) => link.match(dropHeaderOrAlias)[1].split("/").last()); + values.push(...strs); + } + else { + values.push(rawItemAsString.split("/").last()); + } } - } + else if (value.path !== undefined) { + const lastSplit = value.path.split("/").last(); + if (lastSplit !== undefined) { + values.push(lastSplit); + } + } + }); }); } return values; diff --git a/package.json b/package.json index 49383bda..34ace323 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "graphlib": "^2.1.8", "hierarchy-js": "^1.0.4", "juggl-api": "git+https://github.com/HEmile/juggl-api.git", + "nodejs": "^0.0.0", "svelte": "3.35.0" } } diff --git a/src/sharedFunctions.ts b/src/sharedFunctions.ts index 3e6769a5..5ad022b2 100644 --- a/src/sharedFunctions.ts +++ b/src/sharedFunctions.ts @@ -23,6 +23,7 @@ import type { } from "src/interfaces"; import type BreadcrumbsPlugin from "src/main"; import type MatrixView from "src/MatrixView"; +import util from "util"; export function sum(arr: number[]): number { return arr.reduce((a, b) => a + b); @@ -243,33 +244,69 @@ export function getFieldValues( // Dont't add anything, it's not a link // } } else { - const rawValues: (string | number | dvLink | Pos | TFile | undefined)[] = - [rawValuesPreFlat].flat(4); + type RawValue = string | number | dvLink | Pos | TFile | undefined; + const rawValues: (RawValue | typeof Proxy)[] = [rawValuesPreFlat].flat(4); superDebug(settings, `${field} of: ${frontmatterCache?.file?.path}`); superDebug(settings, rawValues); rawValues.forEach((rawItem) => { if (!rawItem) return; - if (typeof rawItem === "string" || typeof rawItem === "number") { - // Obs cache converts link of form: [[\d+]] to number[][] - const rawItemAsString = rawItem.toString(); - const splits = rawItemAsString.match(splitLinksRegex); - if (splits !== null) { - const strs = splits.map((link) => - link.match(dropHeaderOrAlias)[1].split("/").last() - ); - values.push(...strs); - } else { - values.push(rawItemAsString.split("/").last()); - } - } else if (rawItem.path !== undefined) { - superDebug(settings, { rawItem }); - const lastSplit = rawItem.path.split("/").last(); - if (lastSplit !== undefined) { - values.push(lastSplit); - } + + let unProxied = [rawItem]; + if (util.types.isProxy(rawItem)) { + unProxied = []; + + // Definitely a proxy the first time + const firstValue = Object.assign({}, rawItem); + firstValue.values.forEach((firstVal: RawValue | typeof Proxy) => { + if (util.types.isProxy(firstVal)) { + const secondValue = Object.assign({}, firstVal); + const secondValues = secondValue.values; + if (secondValues) { + secondValues.forEach((secondVal: RawValue | typeof Proxy) => { + if (util.types.isProxy(secondVal)) { + const thirdValues = Object.assign({}, secondVal).values; + thirdValues.forEach((thirdVal: RawValue | typeof Proxy) => { + unProxied.push(thirdVal); + }); + } else { + if (typeof secondValues === "string") { + unProxied.push(secondValues); + } else { + unProxied.push(...secondValues); + } + } + }); + } else { + unProxied.push(secondValue); + } + } else { + unProxied.push(firstVal); + } + }); } + unProxied.forEach((value) => { + console.log({ unproxiedValue: value }); + if (typeof value === "string" || typeof value === "number") { + // Obs cache converts link of form: [[\d+]] to number[][] + const rawItemAsString = value.toString(); + const splits = rawItemAsString.match(splitLinksRegex); + if (splits !== null) { + const strs = splits.map((link) => + link.match(dropHeaderOrAlias)[1].split("/").last() + ); + values.push(...strs); + } else { + values.push(rawItemAsString.split("/").last()); + } + } else if (value.path !== undefined) { + const lastSplit = value.path.split("/").last(); + if (lastSplit !== undefined) { + values.push(lastSplit); + } + } + }); }); } return values;