brotli link get/set ok
This commit is contained in:
parent
36bc353ff5
commit
42c7db0810
|
@ -3,6 +3,7 @@
|
||||||
import { shorten } from '$lib/utils'
|
import { shorten } from '$lib/utils'
|
||||||
import Select from './Select.svelte'
|
import Select from './Select.svelte'
|
||||||
import type { Editor } from 'codemirror'
|
import type { Editor } from 'codemirror'
|
||||||
|
import { shareUrl, selectedLang } from '../store'
|
||||||
|
|
||||||
type Language = {
|
type Language = {
|
||||||
text: string
|
text: string
|
||||||
|
@ -11,6 +12,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
export let editor: Editor
|
export let editor: Editor
|
||||||
|
export let updateShareUrl: () => Promise<void>
|
||||||
|
|
||||||
let languages: Language[] = []
|
let languages: Language[] = []
|
||||||
let selectedLanguage: Language | null = null
|
let selectedLanguage: Language | null = null
|
||||||
|
@ -24,50 +26,29 @@
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
editor.setOption('mode', language.mime)
|
editor.setOption('mode', language.mime)
|
||||||
CodeMirror.autoLoadMode(editor, language.mode)
|
CodeMirror.autoLoadMode(editor, language.mode)
|
||||||
|
$selectedLang = selectedLanguage?.value ?? null
|
||||||
}
|
}
|
||||||
|
|
||||||
// const initLangSelector = () => {
|
|
||||||
// select = new SlimSelect({
|
|
||||||
// select: '#language',
|
|
||||||
// data: CodeMirror.modeInfo.map((e) => ({
|
|
||||||
// text: e.name,
|
|
||||||
// value: shorten(e.name),
|
|
||||||
// data: { mime: e.mime, mode: e.mode },
|
|
||||||
// })),
|
|
||||||
// settings: {
|
|
||||||
// openPosition: 'down',
|
|
||||||
// },
|
|
||||||
// events: {
|
|
||||||
// afterChange: (e) => {
|
|
||||||
// console.log(e)
|
|
||||||
// // const language = e.data || { mime: null, mode: null }
|
|
||||||
// // editor.setOption('mode', language.mime)
|
|
||||||
// // CodeMirror.autoLoadMode(editor, language.mode)
|
|
||||||
// // document.title =
|
|
||||||
// // e.text && e.text !== 'Plain Text' ? `NoPaste - ${e.text} code snippet` : 'NoPaste'
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// })
|
|
||||||
|
|
||||||
// // Set lang selector
|
|
||||||
// const l = new URLSearchParams(window.location.search).get('l')
|
|
||||||
// }
|
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
languages = CodeMirror.modeInfo.map((e) => ({
|
languages = CodeMirror.modeInfo.map((e) => ({
|
||||||
text: e.name,
|
text: e.name,
|
||||||
value: shorten(e.name),
|
value: shorten(e.name),
|
||||||
data: { mime: e.mime, mode: e.mode },
|
data: { mime: e.mime, mode: e.mode },
|
||||||
}))
|
}))
|
||||||
// initLangSelector()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
export function setLanguage(lang: string) {
|
||||||
|
const language = languages.find((e) => e.value === lang)!
|
||||||
|
selectedLanguage = language
|
||||||
|
}
|
||||||
|
|
||||||
function setDefaultLanguage(languages: Language[]) {
|
function setDefaultLanguage(languages: Language[]) {
|
||||||
const lang = languages.find((e) => e.text.toLowerCase() === 'plain text')!
|
setLanguage('plt')
|
||||||
selectedLanguage = lang
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function showUrlInput() {
|
async function showUrlInput() {
|
||||||
|
// Make sure the url is up to date
|
||||||
|
await updateShareUrl()
|
||||||
isUrlInputVisible = true
|
isUrlInputVisible = true
|
||||||
await tick()
|
await tick()
|
||||||
urlInput.select()
|
urlInput.select()
|
||||||
|
@ -105,7 +86,7 @@
|
||||||
bind:this={urlInput}
|
bind:this={urlInput}
|
||||||
type="text"
|
type="text"
|
||||||
class="border border-gray-300 bg-transparent p-1 grow"
|
class="border border-gray-300 bg-transparent p-1 grow"
|
||||||
value="url goes here"
|
value={$shareUrl}
|
||||||
/>
|
/>
|
||||||
<button on:click={copyUrl}> Copy </button>
|
<button on:click={copyUrl}> Copy </button>
|
||||||
<button on:click={closeUrlInput}> Close </button>
|
<button on:click={closeUrlInput}> Close </button>
|
||||||
|
|
|
@ -6,12 +6,13 @@
|
||||||
import * as lzma from '$lib/lzma'
|
import * as lzma from '$lib/lzma'
|
||||||
import { byId } from '$lib/utils'
|
import { byId } from '$lib/utils'
|
||||||
import TopBar from '../components/TopBar.svelte'
|
import TopBar from '../components/TopBar.svelte'
|
||||||
|
import { selectedLang, shareUrl } from '../store'
|
||||||
|
|
||||||
const algorithm: {
|
const algorithm: {
|
||||||
compress: (data: string) => Promise<string>
|
compress: (data: string) => Promise<string>
|
||||||
decompress: (data: string) => Promise<string>
|
decompress: (data: string) => Promise<string>
|
||||||
} = brotli
|
} = brotli
|
||||||
|
|
||||||
const host = window.location.protocol + '//' + window.location.host + '/'
|
const host = window.location.protocol + '//' + window.location.host + '/'
|
||||||
|
|
||||||
let editor: Editor | null = null
|
let editor: Editor | null = null
|
||||||
|
@ -20,13 +21,40 @@
|
||||||
let charLen = 0
|
let charLen = 0
|
||||||
let compressed: string = ''
|
let compressed: string = ''
|
||||||
let waiting = false
|
let waiting = false
|
||||||
$: shareUrl = host + compressed
|
let setLanguage: (lang: string) => void
|
||||||
|
|
||||||
const updateCompressed = debounce(async () => {
|
onMount(async () => {
|
||||||
if (editor) {
|
initCodeEditor()
|
||||||
compressed = await algorithm.compress(editor.getValue())
|
// extract the part in the url after the hash
|
||||||
console.log(compressed)
|
const hash = window.location.hash.slice(1)
|
||||||
|
if (hash) {
|
||||||
|
// decompress the data
|
||||||
|
const decompressed = await algorithm.decompress(hash)
|
||||||
|
// set the editor value
|
||||||
|
if (editor) {
|
||||||
|
editor.setValue(decompressed)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
const lang = new URLSearchParams(window.location.search).get('l')
|
||||||
|
if (lang) {
|
||||||
|
setLanguage(lang)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
async function updateShareUrl() {
|
||||||
|
if (editor) {
|
||||||
|
const url = new URL(window.location.href)
|
||||||
|
compressed = await algorithm.compress(editor.getValue())
|
||||||
|
if ($selectedLang) {
|
||||||
|
url.searchParams.set('l', $selectedLang)
|
||||||
|
}
|
||||||
|
url.hash = compressed
|
||||||
|
$shareUrl = url.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateShareUrlDebounced = debounce(async () => {
|
||||||
|
updateShareUrl()
|
||||||
waiting = false
|
waiting = false
|
||||||
}, 1000)
|
}, 1000)
|
||||||
|
|
||||||
|
@ -39,7 +67,7 @@
|
||||||
lineWrapping: false,
|
lineWrapping: false,
|
||||||
scrollbarStyle: 'native',
|
scrollbarStyle: 'native',
|
||||||
}) as Editor
|
}) as Editor
|
||||||
console.log(editor)
|
|
||||||
if (readOnly) {
|
if (readOnly) {
|
||||||
document.body.classList.add('readonly')
|
document.body.classList.add('readonly')
|
||||||
}
|
}
|
||||||
|
@ -48,19 +76,15 @@
|
||||||
if (editor) {
|
if (editor) {
|
||||||
waiting = true
|
waiting = true
|
||||||
charLen = editor.getValue().length
|
charLen = editor.getValue().length
|
||||||
updateCompressed()
|
updateShareUrlDebounced()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
onMount(async () => {
|
|
||||||
initCodeEditor()
|
|
||||||
})
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="flex flex-col font-mono h-screen bg-gray-700">
|
<div class="flex flex-col font-mono h-screen bg-gray-700">
|
||||||
{#if !!editor}
|
{#if !!editor}
|
||||||
<TopBar {editor} />
|
<TopBar {editor} {updateShareUrl} bind:setLanguage />
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
<div id="editor" class="grow overflow-hidden" />
|
<div id="editor" class="grow overflow-hidden" />
|
||||||
|
@ -68,10 +92,10 @@
|
||||||
|
|
||||||
<div class="p-2 text-sm">
|
<div class="p-2 text-sm">
|
||||||
Data length: {charLen}
|
Data length: {charLen}
|
||||||
| Link length: {waiting ? '?' : shareUrl.length}
|
| Link length: {waiting ? '?' : $shareUrl.length}
|
||||||
({waiting || !compressed.length || !charLen
|
({waiting || !compressed.length || !charLen
|
||||||
? '?'
|
? '?'
|
||||||
: Math.round((shareUrl.length / charLen) * 100)}% of original)
|
: Math.round(($shareUrl.length / charLen) * 100)}% of original)
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
4
src/store.ts
Normal file
4
src/store.ts
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
import { writable } from 'svelte/store'
|
||||||
|
|
||||||
|
export const shareUrl = writable('')
|
||||||
|
export const selectedLang = writable<string | null>(null)
|
Loading…
Reference in New Issue
Block a user