Skip to content

Commit

Permalink
fix!: Upgrade development dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyfarrell committed Oct 16, 2023
1 parent 7e94a4d commit 2b29cba
Show file tree
Hide file tree
Showing 10 changed files with 422 additions and 407 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/release-please.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
on:
push:
branches:
- master

permissions:
contents: write
pull-requests: write

name: release-please

jobs:
release-please:
runs-on: ubuntu-latest
steps:
- uses: google-github-actions/release-please-action@v3
with:
release-type: node
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: 16
node-version: 18
- run: npm install
- name: Lint
run: npm run -s pretest
Expand Down
10 changes: 5 additions & 5 deletions calculate-state.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import defaultState from './default-state.js';

export default (delta, previousState) => {
return {
...defaultState,
...previousState,
index: (history.state?.index || 0) + delta
};
return {
...defaultState,
...previousState,
index: (history.state?.index || 0) + delta
};
};
6 changes: 3 additions & 3 deletions default-state.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default {
index: 0,
dirty: false,
state: null
index: 0,
dirty: false,
state: null
};
174 changes: 87 additions & 87 deletions history-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,124 +9,124 @@ let currentState = defaultState;
let defaultInterceptOptions;

const updateState = (newSettings, title, url) => {
history.replaceState(calculateState(0, {...currentState, ...newSettings}), title, url);
currentState = history.state;
history.replaceState(calculateState(0, {...currentState, ...newSettings}), title, url);
currentState = history.state;
};

class HistoryState extends EventTarget {
replaceState(state, title, url) {
updateState({state}, title, url);
}

pushState(state, title, url) {
history.pushState(calculateState(1, {state}), title, url);
spaUpdate();
}

get state() {
return currentState.state;
}

get length() {
return history.length;
}

get scrollRestoration() {
return history.scrollRestoration;
}

set scrollRestoration(value) {
history.scrollRestoration = value;
}

back() {
history.back();
}

forward() {
history.forward();
}

go(delta) {
history.go(delta);
}
replaceState(state, title, url) {
updateState({state}, title, url);
}

pushState(state, title, url) {
history.pushState(calculateState(1, {state}), title, url);
spaUpdate();
}

get state() {
return currentState.state;
}

get length() {
return history.length;
}

get scrollRestoration() {
return history.scrollRestoration;
}

set scrollRestoration(value) {
history.scrollRestoration = value;
}

back() {
history.back();
}

forward() {
history.forward();
}

go(delta) {
history.go(delta);
}
}

updateState(history.state);

window.addEventListener('load', () => {
if (defaultInterceptOptions !== false) {
linkInterceptor(document, defaultInterceptOptions);
}
if (defaultInterceptOptions !== false) {
linkInterceptor(document, defaultInterceptOptions);
}

spaUpdate(true);
setTimeout(() => window.addEventListener('popstate', onpopstate), 0);
spaUpdate(true);
setTimeout(() => window.addEventListener('popstate', onpopstate), 0);
});

window.addEventListener('beforeunload', event => {
if (currentState.dirty) {
event.preventDefault();
event.returnValue = '';
}
if (currentState.dirty) {
event.preventDefault();
event.returnValue = '';
}
});

const historyState = new HistoryState();

const spaUpdate = initializing => {
if (currentState.dirty && !initializing) {
inRevert = true;
dirtyAttempt = history.state.index - currentState.index;
history.go(-dirtyAttempt);
return;
}

currentState = history.state;
historyState.dispatchEvent(new Event('update'));
if (currentState.dirty && !initializing) {
inRevert = true;
dirtyAttempt = history.state.index - currentState.index;
history.go(-dirtyAttempt);
return;
}

currentState = history.state;
historyState.dispatchEvent(new Event('update'));
};

const onpopstate = () => {
if (inRevert) {
inRevert = false;
historyState.dispatchEvent(new Event('refuse'));
} else {
spaUpdate();
}
if (inRevert) {
inRevert = false;
historyState.dispatchEvent(new Event('refuse'));
} else {
spaUpdate();
}
};

export const navigateTo = url => historyState.pushState(null, '', new URL(url, location.href).href);

export const linkInterceptor = (listener, listenerOptions) => {
listener.addEventListener('click', event => {
if (event.defaultPrevented || !isNormalLeftClick(event)) {
return;
}

const element = event.composedPath().find(element => element.tagName === 'A');
if (!isNormalLink(element)) {
return;
}

const destination = new URL(element.href, location.href).toString();
if (destination.startsWith(document.baseURI.replace(/[?#].*/u, ''))) {
element.blur();
event.preventDefault();
event.stopPropagation();
historyState.pushState(null, '', destination);
}
}, listenerOptions);
listener.addEventListener('click', event => {
if (event.defaultPrevented || !isNormalLeftClick(event)) {
return;
}

const element = event.composedPath().find(element => element.tagName === 'A');
if (!isNormalLink(element)) {
return;
}

const destination = new URL(element.href, location.href).toString();
if (destination.startsWith(document.baseURI.replace(/[?#].*/u, ''))) {
element.blur();
event.preventDefault();
event.stopPropagation();
historyState.pushState(null, '', destination);
}
}, listenerOptions);
};

export const setDefaultInterceptOptions = options => {
defaultInterceptOptions = options;
defaultInterceptOptions = options;
};

export const bypassDirty = () => {
if (dirtyAttempt !== 0) {
const attempt = dirtyAttempt;
dirtyAttempt = 0;
currentState.dirty = false;
history.go(attempt);
}
if (dirtyAttempt !== 0) {
const attempt = dirtyAttempt;
dirtyAttempt = 0;
currentState.dirty = false;
history.go(attempt);
}
};

export const setDirty = dirty => updateState({dirty});
Expand Down
18 changes: 9 additions & 9 deletions is-normal-link.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export default node => {
if (node?.tagName !== 'A' || !node.href) {
return false;
}
if (node?.tagName !== 'A' || !node.href) {
return false;
}

for (const bannedAttribute of ['download', 'target', 'no-history-state']) {
if (node.hasAttribute(bannedAttribute)) {
return false;
}
}
for (const bannedAttribute of ['download', 'target', 'no-history-state']) {
if (node.hasAttribute(bannedAttribute)) {
return false;
}
}

return true;
return true;
};
3 changes: 0 additions & 3 deletions nyc.config.cjs

This file was deleted.

1 change: 1 addition & 0 deletions nyc.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {default} from '@cfware/nyc';
76 changes: 38 additions & 38 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
{
"name": "@cfware/history-state",
"version": "0.5.0",
"description": "Browser History API state manager",
"main": "history-state.js",
"type": "module",
"scripts": {
"pretest": "cfware-lint .",
"tests-only": "nyc -s node test.js|tap-yaml-summary",
"test": "npm run -s tests-only",
"posttest": "nyc report --check-coverage"
},
"engines": {
"node": ">=16.12.0"
},
"author": "Corey Farrell",
"license": "MIT",
"keywords": [
"spa",
"history-api",
"browser"
],
"repository": {
"type": "git",
"url": "git+https://github.com/cfware/history-state.git"
},
"bugs": {
"url": "https://github.com/cfware/history-state/issues"
},
"homepage": "https://github.com/cfware/history-state#readme",
"devDependencies": {
"@cfware/fastify-test-helper": "^0.8.0",
"@cfware/lint": "^3.0.4",
"@cfware/nyc": "^0.7.1",
"@cfware/tap-selenium-manager": "^2.0.0",
"libtap": "^1.4.0",
"nyc": "^15.1.0",
"tap-yaml-summary": "^0.2.0"
}
"name": "@cfware/history-state",
"version": "0.5.0",
"description": "Browser History API state manager",
"main": "history-state.js",
"type": "module",
"scripts": {
"pretest": "cfware-lint .",
"tests-only": "nyc -s node test.js|tap-yaml-summary",
"test": "npm run -s tests-only",
"posttest": "nyc report --check-coverage"
},
"engines": {
"node": ">=18"
},
"author": "Corey Farrell",
"license": "MIT",
"keywords": [
"spa",
"history-api",
"browser"
],
"repository": {
"type": "git",
"url": "git+https://github.com/cfware/history-state.git"
},
"bugs": {
"url": "https://github.com/cfware/history-state/issues"
},
"homepage": "https://github.com/cfware/history-state#readme",
"devDependencies": {
"@cfware/fastify-test-helper": "^1",
"@cfware/lint": "^4",
"@cfware/nyc": "^1",
"@cfware/tap-selenium-manager": "^3",
"libtap": "^1",
"nyc": "^15",
"tap-yaml-summary": "^0.2"
}
}
Loading

0 comments on commit 2b29cba

Please sign in to comment.