Skip to content

Commit de72718

Browse files
author
wanglin25
committed
Feat:支持在未配置github gist的情况下将变动保存到url中
1 parent ae771c2 commit de72718

File tree

5 files changed

+67
-6
lines changed

5 files changed

+67
-6
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636

3737
- [x] 内置支持保存到github gist【[gist API](https://docs.github.com/cn/rest/reference/gists)】,想要保存到自己的存储体系修改也十分简单
3838

39+
- [x] 内置支持在未配置github gist的情况下将变动保存到url中,可方便的将url分享给他人查看
40+
3941
- [x] 内置支持生成和[carbon](https://carbon.now.sh/)一样漂亮美观的代码图片
4042

4143
- [x] 内置使用[unpkg](https://unpkg.com/)[importmap](https://github.com/WICG/import-maps)支持在浏览器上使用ES模块语法

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"dayjs": "^1.10.7",
4242
"element-plus": "^1.1.0-beta.12",
4343
"eventemitter3": "^4.0.7",
44+
"fflate": "^0.8.0",
4445
"html2canvas": "^1.3.2",
4546
"jszip": "^3.7.1",
4647
"livescript": "^1.6.0",

src/pages/edit/Index.vue

+30-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { layoutMap, defaultViewThemeConfig } from '@/config/constants'
2323
import { useRouter, useRoute } from 'vue-router'
2424
import { initMonacoEditor } from '@/utils/monacoEditor'
2525
import nprogress from 'nprogress'
26-
import { getThemeValue } from '@/utils'
26+
import { getThemeValue, utoa } from '@/utils'
2727
2828
const props = defineProps({
2929
// 是否是嵌入模式
@@ -48,7 +48,10 @@ const useInit = () => {
4848
// 获取当前编辑数据
4949
const getData = async () => {
5050
nprogress.start()
51-
await store.dispatch('getData', route.params.id)
51+
await store.dispatch('getData', {
52+
id: route.params.id,
53+
data: route.query.data
54+
})
5255
proxy.$eventEmitter.emit('reset_code')
5356
nprogress.done()
5457
}
@@ -57,8 +60,10 @@ const useInit = () => {
5760
() => {
5861
return route.params
5962
},
60-
() => {
61-
getData()
63+
(newVal, oldVal) => {
64+
if (newVal.params.id !== oldVal.params.id) {
65+
getData()
66+
}
6267
}
6368
)
6469
getData()
@@ -71,6 +76,26 @@ const useInit = () => {
7176
}
7277
}
7378
79+
// 将数据保存到query里
80+
const useQueryStore = (store, router) => {
81+
watch(() => {
82+
return store.state.editData
83+
},() => {
84+
if (store.state.githubToken) {
85+
return
86+
}
87+
let data = utoa(JSON.stringify(store.state.editData))
88+
router.replace({
89+
name: 'Editor',
90+
query: {
91+
data
92+
}
93+
})
94+
}, {
95+
deep: true
96+
})
97+
}
98+
7499
// 布局
75100
const useLayout = ({ store }) => {
76101
const layout = computed(() => {
@@ -210,6 +235,7 @@ const useTheme = ({ proxy, store, layout }) => {
210235
211236
// created部分
212237
const { proxy, router, store, init, showContent } = useInit()
238+
useQueryStore(store, router)
213239
const { layout, activeLayout } = useLayout({ store })
214240
const { themeData } = useTheme({ proxy, store, layout })
215241
const { previewLayoutHandle } = useWindowPreview({

src/store.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createStore } from 'vuex'
2-
import { generateUUID } from '@/utils'
2+
import { generateUUID, atou } from '@/utils'
33
import { create, request } from '@/utils/octokit'
44
// 存储github token的本地存储的key
55
const githubTokenSaveKey = 'codeRun:githubToken'
@@ -214,7 +214,7 @@ const store = createStore({
214214
* @Date: 2021-05-12 19:49:17
215215
* @Desc: 获取数据
216216
*/
217-
getData(ctx, id) {
217+
getData(ctx, { id, data }) {
218218
return new Promise(async resolve => {
219219
try {
220220
let parseData = createDefaultData()
@@ -223,6 +223,8 @@ const store = createStore({
223223
gist_id: id
224224
})
225225
parseData = JSON.parse(data.files['coderun.json'].content)
226+
} else if (data) {
227+
parseData = JSON.parse(atou(data))
226228
}
227229
ctx.commit('setEditData', parseData)
228230
resolve()

src/utils/index.js

+30
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { load } from '@/utils/load'
22
import transform from '@/utils/transform'
33
import { routerMode, base } from '@/config'
44
import { defaultViewThemeConfig } from '@/config/constants'
5+
import { zlibSync, strToU8, strFromU8, unzlibSync } from 'fflate'
56

67
/**
78
* javascript comment
@@ -182,3 +183,32 @@ export const getThemeValue = (item, data, pageThemeSyncCodeTheme) => {
182183
}
183184
return arr[len - 1]
184185
}
186+
187+
// 压缩数据
188+
export const utoa = (data) => {
189+
// 将字符串转成Uint8Array
190+
const buffer = strToU8(data)
191+
// 以最大的压缩级别进行压缩,返回的zipped也是一个Uint8Array
192+
const zipped = zlibSync(buffer, { level: 9 })
193+
// 将Uint8Array重新转换成二进制字符串
194+
const binary = strFromU8(zipped, true)
195+
// 将二进制字符串编码为Base64编码字符串
196+
return btoa(binary)
197+
}
198+
199+
// 解压缩数据
200+
export const atou = (base64) => {
201+
// 将base64转成二进制字符串
202+
const binary = atob(base64)
203+
// 检查是否是zlib压缩的数据,zlib header (x78), level 9 (xDA)
204+
if (binary.startsWith('\x78\xDA')) {
205+
// 将字符串转成Uint8Array
206+
const buffer = strToU8(binary, true)
207+
// 解压缩
208+
const unzipped = unzlibSync(buffer)
209+
// 将Uint8Array重新转换成字符串
210+
return strFromU8(unzipped)
211+
}
212+
// 兼容没有使用压缩的数据
213+
return decodeURIComponent(escape(binary))
214+
}

0 commit comments

Comments
 (0)