@keenmate/svelte-spa-router 1.0.8 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/Router.svelte +44 -35
  2. package/package.json +1 -1
package/Router.svelte CHANGED
@@ -218,14 +218,16 @@
218
218
  throw Error("Action 'link' can only be used with <a> tags")
219
219
  }
220
220
 
221
- updateLink(node, opts)
221
+ const self = {}
222
222
 
223
- return {
224
- update(updated) {
225
- updated = linkOpts(updated)
226
- updateLink(node, updated)
227
- }
223
+ self.update = function(updated) {
224
+ updated = linkOpts(updated)
225
+ updateLink.call(self, node, updated)
228
226
  }
227
+
228
+ updateLink.call(self, node, opts)
229
+
230
+ return self
229
231
  }
230
232
 
231
233
  /**
@@ -248,7 +250,7 @@
248
250
  function updateLink(node, opts) {
249
251
  const basePath = get(BasePath)
250
252
 
251
- let href = opts.href || node.getAttribute("href")
253
+ let href = opts.href || node.dataset["href"]
252
254
 
253
255
  if (get(HashRoutingEnabled)) {
254
256
  if (href && href.charAt(0) == "/") {
@@ -266,39 +268,46 @@
266
268
  }
267
269
 
268
270
  node.setAttribute("href", href)
269
- node.addEventListener("click", ev => {
270
- if (get(HashRoutingEnabled)) {
271
- // Prevent default anchor onclick behaviour
272
- ev.preventDefault()
273
- if (!opts.disabled) {
274
- scrollstateHistoryHandler(ev.currentTarget.getAttribute("href"))
275
- }
276
- }
277
- else {
278
- // console.log("Handling link click event")
279
- const linkTarget = ev.target.getAttribute("target")
280
- if (linkTarget && linkTarget !== "_self") {
281
- // console.log("Link has special target attr, opening href instead")
282
- ev.preventDefault()
283
- // prevent pushState when link is perhaps going outside of this window
284
- window.open(node.getAttribute("href"), linkTarget)
285
271
 
286
- return
287
- }
288
- if (ev.ctrlKey || ev.shiftKey || ev.metaKey) {
289
- // console.log("Modifier key has been held while opening link, keeping behaviour intact")
290
- return
291
- }
272
+ if (this.specialOnLinkClicked) {
273
+ node.removeEventListener("click", this.specialOnLinkClicked)
274
+ }
275
+ this.specialOnLinkClicked = ev => onLinkClicked(ev, node, opts)
276
+ node.addEventListener("click", this.specialOnLinkClicked)
277
+ }
292
278
 
293
- // console.log("Custom link operation is in place")
279
+ function onLinkClicked(ev, node, opts) {
280
+ if (get(HashRoutingEnabled)) {
281
+ // Prevent default anchor onclick behaviour
282
+ ev.preventDefault()
283
+ if (!opts.disabled) {
284
+ scrollstateHistoryHandler(ev.currentTarget.getAttribute("href"))
285
+ }
286
+ }
287
+ else {
288
+ // console.log("Handling link click event")
289
+ const linkTarget = ev.target.getAttribute("target")
290
+ if (linkTarget && linkTarget !== "_self") {
291
+ // console.log("Link has special target attr, opening href instead")
294
292
  ev.preventDefault()
293
+ // prevent pushState when link is perhaps going outside of this window
294
+ window.open(node.getAttribute("href"), linkTarget)
295
295
 
296
- const shouldReplace = typeof opts !== "string" && opts.shouldReplace
297
-
298
- jediForcePush(node.getAttribute("href"), shouldReplace)
299
- // window.dispatchEvent(new Event(SvelteSPARouterNavigationEvent))
296
+ return
300
297
  }
301
- })
298
+ if (ev.ctrlKey || ev.shiftKey || ev.metaKey) {
299
+ // console.log("Modifier key has been held while opening link, keeping behaviour intact")
300
+ return
301
+ }
302
+
303
+ // console.log("Custom link operation is in place")
304
+ ev.preventDefault()
305
+
306
+ const shouldReplace = typeof opts !== "string" && opts.shouldReplace
307
+
308
+ jediForcePush(node.getAttribute("href"), shouldReplace)
309
+ // window.dispatchEvent(new Event(SvelteSPARouterNavigationEvent))
310
+ }
302
311
  }
303
312
 
304
313
  // Internal function that ensures the argument of the link action is always an object
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@keenmate/svelte-spa-router",
3
3
  "private": false,
4
- "version": "1.0.8",
4
+ "version": "2.0.1",
5
5
  "description": "Router for SPAs using Svelte 4",
6
6
  "main": "Router.svelte",
7
7
  "svelte": "Router.svelte",