move code hashing helpers to core
This commit is contained in:
parent
87feac447b
commit
99194814a9
3 changed files with 38 additions and 27 deletions
|
|
@ -274,3 +274,31 @@ export const sol2note = (n, notation = 'letters') => {
|
||||||
const oct = Math.floor(n / 12) - 1;
|
const oct = Math.floor(n / 12) - 1;
|
||||||
return note + oct;
|
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 || ''));
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,16 @@ Copyright (C) 2022 Strudel contributors - see <https://github.com/tidalcycles/st
|
||||||
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
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 { CodeMirror, cx, flash, useHighlighting, useStrudel, useKeydown } from '@strudel.cycles/react';
|
||||||
import { getAudioContext, initAudioOnFirstClick, resetLoadedSounds, webaudioOutput } from '@strudel.cycles/webaudio';
|
import { getAudioContext, initAudioOnFirstClick, resetLoadedSounds, webaudioOutput } from '@strudel.cycles/webaudio';
|
||||||
import { createClient } from '@supabase/supabase-js';
|
import { createClient } from '@supabase/supabase-js';
|
||||||
|
|
@ -29,7 +38,6 @@ import {
|
||||||
} from '../settings.mjs';
|
} from '../settings.mjs';
|
||||||
import Loader from './Loader';
|
import Loader from './Loader';
|
||||||
import { settingPatterns } from '../settings.mjs';
|
import { settingPatterns } from '../settings.mjs';
|
||||||
import { code2hash, hash2code } from './helpers.mjs';
|
|
||||||
import { isTauri } from '../tauri.mjs';
|
import { isTauri } from '../tauri.mjs';
|
||||||
import { useWidgets } from '@strudel.cycles/react/src/hooks/useWidgets.mjs';
|
import { useWidgets } from '@strudel.cycles/react/src/hooks/useWidgets.mjs';
|
||||||
import { writeText } from '@tauri-apps/api/clipboard';
|
import { writeText } from '@tauri-apps/api/clipboard';
|
||||||
|
|
|
||||||
|
|
@ -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 || ''));
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue