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 1
1
import { data as allRules } from '../../_data/catalog.data'
2
2
export type { RuleMeta } from '../../_data/catalog.data'
3
+ import { atou , utoa } from '../utils'
3
4
4
5
export function intersect ( a : string [ ] , b : string [ ] ) {
5
6
return a . some ( x => b . includes ( x ) )
@@ -84,4 +85,25 @@ export const features = [
84
85
'transform' ,
85
86
'constraints' ,
86
87
'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 1
1
<script setup lang="ts">
2
- import { ref } from ' vue'
2
+ import { ref , watch } from ' vue'
3
3
import RuleFilter from ' ./RuleFilter.vue'
4
4
import RuleList from ' ./RuleList.vue'
5
5
import type { Filter } from ' ./data' ;
6
+ import { serialize , deserialize } from ' ./data' ;
6
7
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 ,
12
18
})
13
19
14
20
function reset() {
Original file line number Diff line number Diff line change 1
1
import type { SupportedLang } from "./lang"
2
2
import { shallowReactive , toRefs , watch , provide } from 'vue'
3
3
import type { InjectionKey , ToRefs , ShallowReactive } from 'vue'
4
+ import { utoa , atou } from '../../utils'
4
5
5
6
export enum Mode {
6
7
Patch = 'Patch' ,
@@ -18,16 +19,6 @@ export type State = {
18
19
lang : SupportedLang ,
19
20
}
20
21
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
-
31
22
export function serialize ( state : State ) : string {
32
23
return utoa ( JSON . stringify ( state ) )
33
24
}
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