From 99194814a98126ba1347c01496972be744afeaf6 Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Thu, 14 Dec 2023 10:36:56 +0100 Subject: [PATCH] move code hashing helpers to core --- packages/core/util.mjs | 28 ++++++++++++++++++++++++++++ website/src/repl/Repl.jsx | 12 ++++++++++-- website/src/repl/helpers.mjs | 25 ------------------------- 3 files changed, 38 insertions(+), 27 deletions(-) delete mode 100644 website/src/repl/helpers.mjs diff --git a/packages/core/util.mjs b/packages/core/util.mjs index 695eaacf..2b190c82 100644 --- a/packages/core/util.mjs +++ b/packages/core/util.mjs @@ -274,3 +274,31 @@ export const sol2note = (n, notation = 'letters') => { const oct = Math.floor(n / 12) - 1; return note + oct; }; + +// code hashing helpers + +export function unicodeToBase64(text) { + const utf8Bytes = new TextEncoder().encode(text); + const base64String = btoa(String.fromCharCode(...utf8Bytes)); + return base64String; +} + +export function base64ToUnicode(base64String) { + const utf8Bytes = new Uint8Array( + atob(base64String) + .split('') + .map((char) => char.charCodeAt(0)), + ); + const decodedText = new TextDecoder().decode(utf8Bytes); + return decodedText; +} + +export function code2hash(code) { + return encodeURIComponent(unicodeToBase64(code)); + //return '#' + encodeURIComponent(btoa(code)); +} + +export function hash2code(hash) { + return base64ToUnicode(decodeURIComponent(hash)); + //return atob(decodeURIComponent(codeParam || '')); +} diff --git a/website/src/repl/Repl.jsx b/website/src/repl/Repl.jsx index a606d67a..d4f85434 100644 --- a/website/src/repl/Repl.jsx +++ b/website/src/repl/Repl.jsx @@ -4,7 +4,16 @@ Copyright (C) 2022 Strudel contributors - see . */ -import { cleanupDraw, cleanupUi, controls, evalScope, getDrawContext, logger } from '@strudel.cycles/core'; +import { + cleanupDraw, + cleanupUi, + controls, + evalScope, + getDrawContext, + logger, + code2hash, + hash2code, +} from '@strudel.cycles/core'; import { CodeMirror, cx, flash, useHighlighting, useStrudel, useKeydown } from '@strudel.cycles/react'; import { getAudioContext, initAudioOnFirstClick, resetLoadedSounds, webaudioOutput } from '@strudel.cycles/webaudio'; import { createClient } from '@supabase/supabase-js'; @@ -29,7 +38,6 @@ import { } from '../settings.mjs'; import Loader from './Loader'; import { settingPatterns } from '../settings.mjs'; -import { code2hash, hash2code } from './helpers.mjs'; import { isTauri } from '../tauri.mjs'; import { useWidgets } from '@strudel.cycles/react/src/hooks/useWidgets.mjs'; import { writeText } from '@tauri-apps/api/clipboard'; diff --git a/website/src/repl/helpers.mjs b/website/src/repl/helpers.mjs deleted file mode 100644 index b86e76f1..00000000 --- a/website/src/repl/helpers.mjs +++ /dev/null @@ -1,25 +0,0 @@ -export function unicodeToBase64(text) { - const utf8Bytes = new TextEncoder().encode(text); - const base64String = btoa(String.fromCharCode(...utf8Bytes)); - return base64String; -} - -export function base64ToUnicode(base64String) { - const utf8Bytes = new Uint8Array( - atob(base64String) - .split('') - .map((char) => char.charCodeAt(0)), - ); - const decodedText = new TextDecoder().decode(utf8Bytes); - return decodedText; -} - -export function code2hash(code) { - return encodeURIComponent(unicodeToBase64(code)); - //return '#' + encodeURIComponent(btoa(code)); -} - -export function hash2code(hash) { - return base64ToUnicode(decodeURIComponent(hash)); - //return atob(decodeURIComponent(codeParam || '')); -}