2016-10-10 11 views
0

私は動的であるsvgテキストの塗りつぶしを持っています。ユーザーが取り消しボタンをクリックすると、それはsvgとtextareaを元に戻す必要があり、ユーザーがやり直しボタンをクリックすると、svgテキストの塗りつぶしとテキストエリアをやり直す必要があります。私は、テキストエリアに完了して元に戻すと機能をやり直しができますが、アンドゥ/リドゥについてのあなたに私の最後の返信に気づいた場合ではないSVG要素でsvg要素のonclickを元に戻してやり直すには?

$("#enter-text").on("keypress",function(){ 
$("#svg_id").html($(this).val()); 
    }) 

//this value is kept small for testing purposes, you'd probably want to use sth. between 50 and 200 
const stackSize = 10; 

    //left and right define the first and last "index" you can actually  navigate to, a frame with maximum stackSize-1 items between them. 
    //These values are continually growing as you push new states to the stack, so that the index has to be clamped to the actual index in stack by  %stackSize. 
    var stack = Array(stackSize), left=0, right=0, index = 0, timeout; 
//push the first state to the stack, usually an empty string, but not necessarily 
stack[0] = $("#enter-text").val(); 
updateButtons(); 

$("#enter-text").on("keydown keyup change", detachedUpdateText); 
$("#undo").on("click", undo); 
$("#redo").on("click", redo); 

//detach update 
function detachedUpdateText(){ 
    clearTimeout(timeout); 
    timeout = setTimeout(updateText, 500); 
} 

    function updateButtons(){ 
    //disable buttons if the index reaches the respective border of the  frame 
    //write the amount of steps availabe in each direction into the data- count attribute, to be processed by css 
     $("#undo") 
     .prop("disabled", index === left) 
     .attr("data-count", index-left); 

     $("#redo") 
     .prop("disabled", index === right) 
      .attr("data-count", right-index); 

    //show status 
     $("#stat").html(JSON.stringify({ 
     left, 
     right, 
     index, 
     "index in stack": index % stackSize, 
     stack 
    }, null, 4)) 
} 

function updateText(){ 
    var val = $("#enter-text").val().trimRight(); 
    //skip if nothing really changed 
    if(val === stack[index % stackSize]) return; 

    //add value 
    stack[++index % stackSize] = val; 

    //clean the undo-part of the stack 
    while(right > index) 
     stack[right-- % stackSize] = null; 

    //update boundaries 
    right = index; 
    left = Math.max(left, right+1-stackSize); 

    updateButtons(); 
} 

function undo(){ 
    if(index > left){ 
     $("#enter-text").val(stack[--index % stackSize]); 
     updateButtons(); 
    } 
    } 

    function redo(){ 
    if(index < right){ 
     $("#enter-text").val(stack[++index % stackSize]); 
     updateButtons(); 
    } 
    } 

https://jsfiddle.net/yvp3jedr/6/

+0

わからないjqueryのを介してこれを実現する方法。 2回目の答えは、これを少し簡単にするためにアンドゥとリドゥのロジックを解除したクラスのバージョンを作成しました。 http://stackoverflow.com/questions/39894830/how-to-undo-and-redo-event-in-javascript-with-browser-compatible/39897359#39897359元に戻す/やり直しについて本当に知る必要があることは、 2つのこと、私は "やる"ために何をしましたか、 "元に戻す"(IOW:私がやったことを逆にする)ためには何が必要でしょうか。また、入力からSVGにテキストをコピーするだけなので、あなたがしたいSVGとは何ですか? – Keith

+0

@keith私の元に戻すやり直し機能はうまくいきますが、今はtextarea値がsvg要素に動的に埋め込まれます。だから、元に戻すこともsvgのために行われるべきです。それはtshirtカスタム製品のデザインです。 – Parker

+0

私はあなたのSVGを更新していると仮定しています。現在、あなたのコードはSVGのすべてをTextAreaに置き換えているだけです。 – Keith

答えて

0
$("#enter-text").on("keypress", function() { 
    $("#svg_id").text($(this).val()); 
}) 

//this value is kept small for testing purposes, you'd probably want to use sth. between 50 and 200 
const stackSize = 10; 

//left and right define the first and last "index" you can actually navigate to, a frame with maximum stackSize-1 items between them. 
//These values are continually growing as you push new states to the stack, so that the index has to be clamped to the actual index in stack by %stackSize. 
var stack = Array(stackSize), left = 0, right = 0, index = 0, timeout; 
//push the first state to the stack, usually an empty string, but not necessarily 
stack[0] = $("#enter-text").val(); 
updateButtons(); 

$("#enter-text").on("keydown keyup change", detachedUpdateText); 
$("#undo").on("click", undo); 
$("#redo").on("click", redo); 

//detach update 
function detachedUpdateText() { 
    clearTimeout(timeout); 
    timeout = setTimeout(updateText, 500); 
} 

function updateButtons() { 
    //disable buttons if the index reaches the respective border of the frame 
    //write the amount of steps availabe in each direction into the data-count attribute, to be processed by css 
    $("#undo") 
     .prop("disabled", index === left) 
     .attr("data-count", index - left); 

    $("#redo") 
     .prop("disabled", index === right) 
     .attr("data-count", right - index); 

    //show status 
    $("#stat").html(JSON.stringify({ 
     left, 
     right, 
     index, 
     "index in stack": index % stackSize, 
     stack 
    }, null, 4)) 
} 

function updateText() { 
    var val = $("#enter-text").val().trimRight(); 
    //skip if nothing really changed 
    if (val === stack[index % stackSize]) return; 

    //add value 
    stack[++index % stackSize] = val; 

    //clean the undo-part of the stack 
    while (right > index) 
     stack[right-- % stackSize] = null; 

    //update boundaries 
    right = index; 
    left = Math.max(left, right + 1 - stackSize); 

    updateButtons(); 
} 

function undo() { 
    if (index > left) { 
     var text = stack[--index % stackSize]; 
     $("#enter-text").val(text); 
     $("#svg_id").text(text); 
     updateButtons(); 
    } 
} 

function redo() { 
    if (index < right) { 
     var text = stack[++index % stackSize]; 
     $("#enter-text").val(text); 
     $("#svg_id").text(text); 
     updateButtons(); 
    } 
} 
+0

svgは動的テキストの入力になります – Parker

+0

http://wordpress.tshirtecommerce.com/ click tshirt – Parker

関連する問題