File tree Expand file tree Collapse file tree 4 files changed +45
-17
lines changed
Expand file tree Collapse file tree 4 files changed +45
-17
lines changed Original file line number Diff line number Diff line change 11import { data as allRules } from '../../_data/catalog.data'
22export type { RuleMeta } from '../../_data/catalog.data'
3+ import { atou , utoa } from '../utils'
34
45export function intersect ( a : string [ ] , b : string [ ] ) {
56 return a . some ( x => b . includes ( x ) )
@@ -84,4 +85,25 @@ export const features = [
8485 'transform' ,
8586 'constraints' ,
8687 'utils'
87- ]
88+ ]
89+
90+ export function serialize ( data : Filter ) : string {
91+ const allEmpty = Object . values ( data ) . every ( x => ! x . length )
92+ if ( allEmpty ) {
93+ return ''
94+ }
95+ return utoa ( JSON . stringify ( data ) )
96+ }
97+
98+ export function deserialize ( str : string ) : Filter {
99+ try {
100+ return JSON . parse ( atou ( str ) )
101+ } catch {
102+ return {
103+ selectedLanguages : [ ] ,
104+ selectedRules : [ ] ,
105+ selectedFeatures : [ ] ,
106+ selectedTypes : [ ] ,
107+ }
108+ }
109+ }
Original file line number Diff line number Diff line change 11<script setup lang="ts">
2- import { ref } from ' vue'
2+ import { ref , watch } from ' vue'
33import RuleFilter from ' ./RuleFilter.vue'
44import RuleList from ' ./RuleList.vue'
55import type { Filter } from ' ./data' ;
6+ import { serialize , deserialize } from ' ./data' ;
67
7- const filter = ref <Filter >({
8- selectedLanguages: [],
9- selectedRules: [],
10- selectedFeatures: [],
11- selectedTypes: [],
8+ const filter = ref <Filter >(deserialize (
9+ location .hash .slice (1 ) || ' '
10+ ))
11+
12+ watch (filter , (value ) => {
13+ const hash = serialize (value )
14+ history .replaceState ({}, ' ' , hash ? ` #${hash } ` : ' /catalog' )
15+ }, {
16+ deep: true ,
17+ immediate: true ,
1218})
1319
1420function reset() {
Original file line number Diff line number Diff line change 11import type { SupportedLang } from "./lang"
22import { shallowReactive , toRefs , watch , provide } from 'vue'
33import type { InjectionKey , ToRefs , ShallowReactive } from 'vue'
4+ import { utoa , atou } from '../../utils'
45
56export enum Mode {
67 Patch = 'Patch' ,
@@ -18,16 +19,6 @@ export type State = {
1819 lang : SupportedLang ,
1920}
2021
21- // prefer old unicode hacks for backward compatibility
22- // https://base64.guru/developers/javascript/examples/unicode-strings
23- function utoa ( data : string ) : string {
24- return btoa ( unescape ( encodeURIComponent ( data ) ) )
25- }
26-
27- function atou ( base64 : string ) : string {
28- return decodeURIComponent ( escape ( atob ( base64 ) ) )
29- }
30-
3122export function serialize ( state : State ) : string {
3223 return utoa ( JSON . stringify ( state ) )
3324}
Original file line number Diff line number Diff line change 1+ // prefer old unicode hacks for backward compatibility
2+ // https://base64.guru/developers/javascript/examples/unicode-strings
3+ export function utoa ( data : string ) : string {
4+ return btoa ( unescape ( encodeURIComponent ( data ) ) )
5+ }
6+
7+ export function atou ( base64 : string ) : string {
8+ return decodeURIComponent ( escape ( atob ( base64 ) ) )
9+ }
You can’t perform that action at this time.
0 commit comments