Skip to content

Commit

Permalink
feat: add autosave and jumping back, fix some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
delanni committed Apr 13, 2024
1 parent aab57a8 commit 301346f
Showing 1 changed file with 136 additions and 15 deletions.
151 changes: 136 additions & 15 deletions bundle.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -47146,6 +47146,59 @@
localStorage.setItem('variables', JSON.stringify(o));
}

// disable a few bad combos:
document.addEventListener('keydown', (evt) => {
const prefixes = [evt.metaKey && 'Cmd', evt.ctrlKey && 'Ctrl', evt.altKey && 'Alt', evt.shiftKey && 'Shift'].filter(Boolean).sort();
const prefix = prefixes.join('-');
const key = evt.key.toUpperCase();
const combo = prefix ? (prefix + '-' + key) : key;
const preventList = ['Cmd-H', 'Cmd-Q', 'Cmd-[', 'Cmd-O', 'Cmd-P', 'Cmd-S', 'Cmd-W', 'Cmd-T', 'Cmd-N', 'Cmd-M', 'Cmd-A'];

if (preventList.includes(combo)) {
evt.preventDefault();
evt.stopPropagation();
return true;
} else {
return false;
}
});

const memory = {
saveSlot: null,
autoSaves: [],
_autosaveCulling: 10,
_autosaveIndex: 0,

quickSave: (code) => {
memory.saveSlot = code;
},
quickLoad: () => {
return memory.saveSlot;
},
autoSave: (code, force) => {
memory._autosaveIndex+=1;
if (memory._autosaveIndex === memory._autosaveCulling || force) {
memory._autosaveIndex = 0;
const i = memory.autoSaves.push(code);
}
},
jumpBack: (idx = 1) => {
let code;
while (idx-->0 && memory.autoSaves.length) {
code = memory.autoSaves.pop();
console.log("Jumped back. " + memory.autoSaves.length + " more saves");
}
if (memory.autoSaves.length === 0) {
memory.autoSaves = [code];
}
return code;
},
resetAutosaves: () => {
memory.autoSaves = [];
memory._autosaveIndex = 0;
}
}

window.xemitter = emitter;

emitter.on('DOMContentLoaded', function () {
Expand Down Expand Up @@ -47237,21 +47290,25 @@
let automutateInterval = null
emitter.on('editor:toggleAutomutate', (evt, e) => {
// Turn off
clearInterval(automutateInterval);
automutateInterval = null;

const { lastCombo } = evt;
const automutateMode = lastCombo.match(/-(.)$/)?.at(1);

const guessedMsPerBeat = (60000/(bpm||120));

if (automutateMode === '0' || (automutateInterval && automutateMode === 'X')) {
return;
}
const { lastCombo } = evt;

const automutateMode = lastCombo?.match(/-(.)$/)?.at(1);

let mutateTimeout = guessedMsPerBeat;
if (automutateMode === 'X') {
mutateTimeout = Number(prompt("How fast would you like to go? (in milliseconds)", guessedMsPerBeat)) || guessedMsPerBeat;
if (automutateMode === 'X' || !automutateMode) {
if (automutateInterval) {
clearInterval(automutateInterval);
automutateInterval = null;
return;
} else {
mutateTimeout = Number(prompt("How fast would you like to go? (in milliseconds)", guessedMsPerBeat)) || guessedMsPerBeat;
}
} else if (automutateMode === '0') {
clearInterval(automutateInterval);
automutateInterval = null;
return;
} else if (automutateMode === '1') {
mutateTimeout = guessedMsPerBeat;
} else if (automutateMode === '2') {
Expand All @@ -47268,13 +47325,17 @@

const editor = state.editor.editor
const changeTransformChance = 1 - (evt.metaKey ? 0.25 : 0);
clearInterval(automutateInterval);
automutateInterval = setInterval(() => {
const changeTransform = Math.random() > changeTransformChance;
editor.mutator.mutate({ reroll: false, changeTransform });
editor.formatCode();
memory.autoSave(editor.getValue());
}, mutateTimeout);
memory.autoSave(editor.getValue(), true);
});

// TODO: these could be replaced by codemirror combos
emitter.on('editor:commentLine', () => {
const editor = state.editor.editor
const cursorPosition = editor.cm.getCursor();
Expand Down Expand Up @@ -47338,6 +47399,51 @@
})
})

emitter.on('editor:quickSave', () => {
const editor = state.editor.editor
const code = editor.getValue()
repl.eval(code, (string, err) => {
editor.flashCode()
if (!err) sketches.saveLocally(code)
})

memory.quickSave(code);
});

emitter.on('editor:quickLoad', () => {
const editor = state.editor.editor
const code = memory.quickLoad();
if (!code) return;

repl.eval(code, (string, err) => {
editor.flashCode()
if (!err) sketches.saveLocally(code)
})
});

emitter.on('editor:jumpBack1', (evt) => {
const code = memory.jumpBack(1);
if (!code) return;
const editor = state.editor.editor;

repl.eval(code, (string, err) => {
editor.flashCode()
if (!err) sketches.saveLocally(code)
})
});

emitter.on('editor:jumpBack5', (evt) => {
const code = memory.jumpBack(5);
if (!code) return;
const editor = state.editor.editor

editor.setValue(code);
repl.eval(code, (string, err) => {
editor.flashCode()
if (!err) sketches.saveLocally(code)
})
});

emitter.on('editor:evalLine', (line) => {
repl.eval(line)
})
Expand Down Expand Up @@ -47400,6 +47506,7 @@
repl.eval(editor.getValue())
document.title = s.name;
sketches.saveLocally(editor.getValue());
memory.resetAutosaves();
})

// TODO: Copy
Expand Down Expand Up @@ -47542,6 +47649,7 @@
sketchIdx = sketch.index || sketchIdx;
sPut('sketchIdx', sketchIdx);
document.title = sketch.name;
memory.resetAutosaves();
}

document.body.removeChild(autocompleteInput);
Expand Down Expand Up @@ -47777,6 +47885,8 @@
} else {
this.emit(e, this, key)
}

console.log(`[${key}] => ${e}`, this);
})

const opts = {
Expand Down Expand Up @@ -47857,7 +47967,7 @@
if (!start) start = { line: this.cm.firstLine(), ch: 0 }
if (!end) end = { line: this.cm.lastLine() + 1, ch: 0 }
var marker = this.cm.markText(start, end, { className: 'styled-background' })
setTimeout(() => marker.clear(), 300)
setTimeout(() => marker.clear(), 170)
}


Expand Down Expand Up @@ -47899,14 +48009,16 @@
'Ctrl-Enter': 'editor:evalLine',
'Ctrl-/': 'editor:toggleComment',
'Alt-Enter': 'editor:evalBlock',
'Shift-Ctrl-Enter': 'editor:evalAll',
'Shift-Ctrl-G': 'gallery:shareSketch',
'Shift-Ctrl-F': 'editor:formatCode',
'Shift-Ctrl-L': 'gallery:saveToURL',

'Shift-Ctrl-H': 'hideAll',
'Cmd-H': 'hideAll',

'Shift-Ctrl-S': 'screencap',
'Shift-Ctrl-C': 'gallery:search',
'Shift-Ctrl-K': 'editor:commentLine',

// Poop mode
'Shift-Ctrl-X': 'editor:toggleAutomutate', // poop toggle with prompt
'Shift-Ctrl-0': 'editor:toggleAutomutate', // poop off
Expand All @@ -47915,8 +48027,17 @@
'Shift-Ctrl-3': 'editor:toggleAutomutate', // poop 1 bar
'Shift-Ctrl-4': 'editor:toggleAutomutate', // poop 2 bars
'Shift-Ctrl-5': 'editor:toggleAutomutate', // poop 4 bars

// Editor stuff
'Cmd-D': 'editor:duplicateLine',
'Cmd-S': 'editor:evalAll',
'Shift-Ctrl-K': 'editor:commentLine',

// Eval
'Shift-Ctrl-Enter': 'editor:evalAll',
'Cmd-S': 'editor:quickSave',
'Cmd-L': 'editor:quickLoad',
'Cmd-[': 'editor:jumpBack1',
'Cmd-Shift-[': 'editor:jumpBack5',
}
},{}],266:[function(require,module,exports){
var logElement
Expand Down

0 comments on commit 301346f

Please sign in to comment.