From 0af99c5c400e586ad18c818bee54a0beb767dc2e Mon Sep 17 00:00:00 2001 From: Fabien Benetou Date: Tue, 11 Jun 2024 21:57:39 +0200 Subject: [PATCH] better cloning via entity flushToDOM() --- index.html | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/index.html b/index.html index 55ad581..0dbe654 100644 --- a/index.html +++ b/index.html @@ -40,6 +40,8 @@ function getPoly(hashid){ //________________________________________________________________ // used for keywords like LAST / PREVIOUS / ALL let addedContent = [] +let speechCommands = [] +let deletedContent = [] function getAllPrimitives(){ const other_primitives = ["camera", "cursor", "sky", "light", "sound", "videosphere"] @@ -64,8 +66,6 @@ const aframeprimitives = getAllPrimitives() const speechactions = [ "add", "apply", "delete", "clone", "model", "undo" ] const speechcustomcomponents = [ "target", "teleporter" ] -let speechCommands = [] - recognizer.onresult = (event) => { let result = event.results[event.resultIndex] if (result.isFinal) { @@ -91,21 +91,19 @@ recognizer.onresult = (event) => { case speechactions[1] : console.log("recognized apply command") latest.setAttribute( cmd_words[1], cmd_words[2]) // assuming fixed order for now + // should preserve attribute before modification for undoing speechCommands.push( speechContent ) break; case speechactions[2] : + latest.flushToDOM(true) + deletedContent.push( latest.cloneNode(true) ) deleteTarget( latest ) speechCommands.push( speechContent ) addedContent.pop() break; case speechactions[3] : - el = latest.cloneNode(true) // does not work properly, losing some attributes, in particular scale can be problematic - //["scale", "position", "rotation", "wireframe", "target", "material"].map( prop => el.setAttribute(prop, latest.getAttribute(prop) ) ) - //["scale", "position", "rotation", "target" ].map( prop => el.setAttribute(prop, latest.getAttribute(prop) ) ) - el.setAttribute("scale", latest.getAttribute("scale") ) - el.setAttribute("position", latest.getAttribute("position") ) - el.setAttribute("rotation", latest.getAttribute("rotation") ) - el.setAttribute("target", latest.getAttribute("target") ) + latest.flushToDOM(true) + el = latest.cloneNode(true) // seems to preserve most component but somehow not rotation // untested if (cmd_words[1]) console.log('could clone',cmd_words[1],'n times instead') // could optionally add a number of times @@ -123,15 +121,25 @@ recognizer.onresult = (event) => { let prev_cmd_words = speechCommands.at(-1).split(" ").map( i => i.toLowerCase() ) switch(prev_cmd_words[0]) { case speechactions[0]: + case speechactions[3] : + case speechactions[4] : console.log( "undoing", speechCommands.at(-1) ) deleteTarget( latest ) addedContent.pop() break; + case speechactions[2] : + console.log( "undoing", speechCommands.at(-1) ) + addedContent.push( deletedContent.at(-1) ) + AFRAME.scenes[0].appendChild( deletedContent.at(-1) ) + deletedContent.pop() + break; default: console.log( "can't undo", speechCommands.at(-1) ) // note that not all commands might be undo-able } } + // speechCommands.pop() not needed as, for now, undo is not part of the command stack + // to consider for redo break; default: addedContent.push( addNewNoteAsPostItNote(speechContent, "0 1.2 -.5") )