diff --git a/RapidHouseNumbers.js b/RapidHouseNumbers.js index 77c8b61..d2bcb9c 100644 --- a/RapidHouseNumbers.js +++ b/RapidHouseNumbers.js @@ -5,9 +5,9 @@ // ==UserScript== // @name WME Rapid House Numbers -// @description A House Number script with its controls in the House Number mini-editor. It injects the next value in a sequence into each new HN. To support different regions, house numbers may be [0-9]+, [0-9]+[a-z]+, or [0-9]+-[0-9]+. +// @description A House Number script with its controls in the House Number mini-editor. It injects the next value in a sequence into each new HN. All house number formats are supported. // @namespace https://github.com/WazeDev -// @version 2.9 +// @version 3.0 // @include /^https:\/\/(www|beta)\.waze\.com\/(?!user\/)(.{2,6}\/)?editor\/?.*$/ // @copyright 2017-2024, kjg53 // @author kjg53, WazeDev (2023-?), SaiCode (2024-?) @@ -52,12 +52,9 @@ { version: "2.7", message: "Minor version check fix." }, { version: "2.8", message: "Changelog UI enhancements." }, { version: "2.9", message: "Bug fixing." }, + { version: "3.0", message: "Support any house number format." }, ]; - const ALL_DIGITS = /^[0-9]+$/; - const DIG_ALPHA = /^([0-9]+)([A-Z]$)/i; - const DIG_DASH_DIG = /^([0-9]+-)([0-9]+)$/; - const ONE = 49; const NINE = 57; const NUMPAD1 = 97; @@ -171,48 +168,30 @@ window.localStorage.getItem("rapidHNincrement") || 2 ).toString(); - // NOTE: We have two input.rapidHN.next fields because the type property cannot be modified. We, instead, create two fields - // then use a function, updateRapidHNnextVisibility, to determine which one is currently visible. $(addHouseNumberNode).after(`
Next #
- -
-
- - 1,2,3 -
Increment
- +
`); rapidHNtoolbarButton = addHouseNumberNode.nextSibling; - updateRapidHNnextVisibility(false); - + enableDisableControls(rapidHNtoolbarButton, W.map.getZoom() < 18); - $("button#current-input-type").click(() => { - let nextInputType = window.localStorage.getItem("rapidHNnextInputType") || "number"; - - nextInputType = { number: "text", text: "number" }[nextInputType]; - - window.localStorage.setItem("rapidHNnextInputType", nextInputType); - updateRapidHNnextVisibility(true); - }); - // if the key is released blur so that you can type to add a house number rather than see it appended to the next value. $("input.rapidHN.next").keyup(evt => { if (evt.which === 13) { - this.blur(); + evt.target.blur(); } }); @@ -223,7 +202,7 @@ $("div.rapidHN-control input").on("change", () => { const controls = $("div.rapidHN-control"); const rapidHNenabled = $("input.rapidHN.next", controls).filter(":visible").val() - && nonZero($("input.rapidHN.increment", controls)); + && Number($("input.rapidHN.increment", controls).val()) > 0; if (rapidHNenabled) { if (houseNumbersObserver === undefined) { @@ -337,76 +316,41 @@ } function injectHouseNumber(newHouseNumber) { - const incElm = $("input.rapidHN.increment"); - - let inc; - if (oneTimeIncrement) { - inc = oneTimeIncrement; - oneTimeIncrement = undefined; - } else { - inc = parseInt(incElm.val(), 10); - } + let increment = oneTimeIncrement ?? Number($("input.rapidHN.increment").val()); + oneTimeIncrement = undefined; const nextElement = $("input.rapidHN.next").filter(":visible"); const next = nextElement.val(); - if (ALL_DIGITS.test(next)) { - // Inject next HN into WME - // newHouseNumber.val(next).change(); - setNativeValue(newHouseNumber[0], next); - - const n = parseInt(next, 10); - - nextElement.val(n + inc); - } else if (DIG_ALPHA.test(next)) { - // Inject next HN into WME - // newHouseNumber.val(next).change(); - setNativeValue(newHouseNumber[0], next); - - const digAlpha = next.match(DIG_ALPHA); - const curLet = digAlpha[2]; - let min; - let max; - if (curLet >= "a" && curLet <= "z") { - min = "a".codePointAt(0); - max = "z".codePointAt(0); - } else if (curLet >= "A" && curLet <= "Z") { - min = "A".codePointAt(0); - max = "Z".codePointAt(0); - } else { - return; - } + // Inject next HN into WME + setNativeValue(newHouseNumber[0], next); - let nxtLet = curLet.codePointAt(0) + inc; - // if we need to wrap the letter - if (nxtLet > max) { - // Increment the numeric portion - digAlpha[1] = `${parseInt(digAlpha[1], 10) + 1}`; + const nextParts = next.match(/[0-9]+|[a-z]|[A-Z]|\S/g); - // wrap the letter - nxtLet -= max; - nxtLet += min - 1; - } - digAlpha[2] = String.fromCodePoint(nxtLet); + for (const [index, part] of nextParts.reverse().entries()) { + if (!Number.isNaN(Number(part))) { + nextParts[index] = (Number(part) + increment).toString().padStart(part.length, '0'); + break; + } - nextElement.val(digAlpha[1] + digAlpha[2]); - } else if (DIG_DASH_DIG.test(next)) { - // Inject next HN into WME - // newHouseNumber.val(next).change(); - setNativeValue(newHouseNumber[0], next); + if (/[a-z]/i.test(part)) { + let nextLetter = part.codePointAt(0) + (increment % 26); - const digDig = next.match(DIG_DASH_DIG); + increment = Math.floor(increment/26); - // Increment the numeric portion - digDig[2] = `${parseInt(digDig[2], 10) + inc}`; + if ((/[a-z]/.test(part) && nextLetter > 'z'.codePointAt(0)) || + (/[A-Z]/.test(part) && nextLetter > 'Z'.codePointAt(0))) { + nextLetter -= 26; + increment++; + } - nextElement.val(digDig[1] + digDig[2]); + nextParts[index] = String.fromCodePoint(nextLetter); + + if (!increment) break; + } } - } - function nonZero(input) { - const i = parseInt(input.val(), 10); - return !isNaN(i) && i !== 0; + nextElement.val(nextParts.reverse().join('')); } // Type 1-9 instead of 'h' to specify a one-time increment that be applied after the current "next" value is added to the map @@ -481,30 +425,6 @@ return secondary; } - function updateRapidHNnextVisibility(showTooltip) { - const nextInputType = window.localStorage.getItem("rapidHNnextInputType") || "number"; - const inputs = $("input.rapidHN.next"); - - inputs.hide(); - const nextInput = inputs.filter(`[type='${nextInputType}']`); - nextInput.show(); - - $("button#current-input-type").text( - { number: "1", text: "A" }[nextInputType], - ); - - if (showTooltip) { - // hide both tooltips - ["number", "text"].forEach(type => { - const tooltip = $(`span#rapidHN-input-is-${type}`); - tooltip.hide(); - }); - - const tooltip = $(`span#rapidHN-input-is-${nextInputType}`); - tooltip.show(); - } - } - rapidHNBootstrap(); })();