axinya
06-08-2004, 09:16 PM
<!--
Hi out there!
I'm working on an undo-redo function.
Couldnt really find anything useful in the internet.
So I have a try.
Guess it could be quite useful for a lot of people out there
Wonder if the concept is any good?
Please critizise hard!
Try it out if you dare!
Any feedback is welcome!
All suggestions are appreciated!
Yours
Axinya
-->
<html>
<head>
<style type="text/css">
<!--
#wisiwig {
overflow: auto;
position: relative;
height: 300px;
width: 600px;
margin-top: 5px;
padding: 5px;
border: thin groove #999999;
}
-->
</style>
<script language="JavaScript">
<!--
var b_ackup = new Array(); //Store data in Array
var undo_pos= 0;//Position of undo
var flag = false;// shall we start new undo thread and delete obsolete items?
var w_isiwig;//editor interface
function init(){
w_isiwig = document.getElementById("wisiwig");
w_isiwig.contentEditable = true;
w_isiwig.onkeyup = HandleKeys;
b_ackup[0] = w_isiwig.innerHTML;
}
function HandleKeys(evt) {//events to write to backup
//
if (event) {
switch (event.keyCode) {
case 32:
BACKUP();
break;
case 8:
BACKUP();
break;
case 13:
BACKUP();
break;
case 46:
BACKUP();
break;
default:
return;
}
}
}
function BACKUP(){
if (flag){// action if undo was clicked
var removed= b_ackup.splice(flag,b_ackup.length - flag);
for (var i = 0; i < removed.length; i++) {
delete removed[i];//delete obsolete array items
}
b_ackup[b_ackup.length] = w_isiwig.innerHTML;
undo_pos = b_ackup.length - 1;
flag = false;
w_isiwig.focus();
}
else{//write to backup array
b_ackup[b_ackup.length] = w_isiwig.innerHTML;
undo_pos = b_ackup.length - 1;
flag = false;
w_isiwig.focus();
}
}
function undo(){
if (b_ackup.length){//
if(undo_pos==0){w_isiwig.focus();return;}// beginning of backup items
if(flag == false){BACKUP();}// save last state of document
w_isiwig.innerHTML=b_ackup[undo_pos -1];
undo_pos=undo_pos - 1;
flag = undo_pos +1;
look();
w_isiwig.focus();
}
}
function redo(){
if(undo_pos == b_ackup.length -1){w_isiwig.focus();return;}
//end of backup array
w_isiwig.innerHTML=b_ackup[undo_pos +1];
undo_pos=undo_pos + 1;
flag = undo_pos +1;
look();
w_isiwig.focus();
}
function look(){// monitor our Array data
var e='flag:'+flag+'\r\nundo_pos:'+undo_pos+'\r\n';
e+='b_ackup.length:'+b_ackup.length+'\r\n';
for (i=0; i<b_ackup.length; i++){
e+= 'Backup item '+i+':'+b_ackup[i]+'\r\n';
}
document.getElementById('bup_a').value=e;
}
</script>
<title>undo-redo</title>
</head>
<body id = "body" onload= "init();">
<p><b>undo/redo</b> <br>
development version: for <b>IE</b> only<br>
Backup written by <b>'onkeyup'</b> event(space,back,return,del), or
<b>'add to backup'</b> button. </p>
<div id="menu">
<a href="#" onclick='BACKUP();return false';>[add to backup]</a>
<a href="#" onclick='setTimeout("undo()",10)'>[undo]</a>
<a href="#" onclick='setTimeout("redo()",10)'>[redo]</a>
<a href="#" onclick='look()'>[view backup-data]</a> </div>
<div id="wisiwig">Type text: </div>
Backup Array Data: <br>
<div id="bup">
<textarea id="bup_a" name="bup_a" cols="80" rows="6"></textarea>
</div>
</body>
</html>
Hi out there!
I'm working on an undo-redo function.
Couldnt really find anything useful in the internet.
So I have a try.
Guess it could be quite useful for a lot of people out there
Wonder if the concept is any good?
Please critizise hard!
Try it out if you dare!
Any feedback is welcome!
All suggestions are appreciated!
Yours
Axinya
-->
<html>
<head>
<style type="text/css">
<!--
#wisiwig {
overflow: auto;
position: relative;
height: 300px;
width: 600px;
margin-top: 5px;
padding: 5px;
border: thin groove #999999;
}
-->
</style>
<script language="JavaScript">
<!--
var b_ackup = new Array(); //Store data in Array
var undo_pos= 0;//Position of undo
var flag = false;// shall we start new undo thread and delete obsolete items?
var w_isiwig;//editor interface
function init(){
w_isiwig = document.getElementById("wisiwig");
w_isiwig.contentEditable = true;
w_isiwig.onkeyup = HandleKeys;
b_ackup[0] = w_isiwig.innerHTML;
}
function HandleKeys(evt) {//events to write to backup
//
if (event) {
switch (event.keyCode) {
case 32:
BACKUP();
break;
case 8:
BACKUP();
break;
case 13:
BACKUP();
break;
case 46:
BACKUP();
break;
default:
return;
}
}
}
function BACKUP(){
if (flag){// action if undo was clicked
var removed= b_ackup.splice(flag,b_ackup.length - flag);
for (var i = 0; i < removed.length; i++) {
delete removed[i];//delete obsolete array items
}
b_ackup[b_ackup.length] = w_isiwig.innerHTML;
undo_pos = b_ackup.length - 1;
flag = false;
w_isiwig.focus();
}
else{//write to backup array
b_ackup[b_ackup.length] = w_isiwig.innerHTML;
undo_pos = b_ackup.length - 1;
flag = false;
w_isiwig.focus();
}
}
function undo(){
if (b_ackup.length){//
if(undo_pos==0){w_isiwig.focus();return;}// beginning of backup items
if(flag == false){BACKUP();}// save last state of document
w_isiwig.innerHTML=b_ackup[undo_pos -1];
undo_pos=undo_pos - 1;
flag = undo_pos +1;
look();
w_isiwig.focus();
}
}
function redo(){
if(undo_pos == b_ackup.length -1){w_isiwig.focus();return;}
//end of backup array
w_isiwig.innerHTML=b_ackup[undo_pos +1];
undo_pos=undo_pos + 1;
flag = undo_pos +1;
look();
w_isiwig.focus();
}
function look(){// monitor our Array data
var e='flag:'+flag+'\r\nundo_pos:'+undo_pos+'\r\n';
e+='b_ackup.length:'+b_ackup.length+'\r\n';
for (i=0; i<b_ackup.length; i++){
e+= 'Backup item '+i+':'+b_ackup[i]+'\r\n';
}
document.getElementById('bup_a').value=e;
}
</script>
<title>undo-redo</title>
</head>
<body id = "body" onload= "init();">
<p><b>undo/redo</b> <br>
development version: for <b>IE</b> only<br>
Backup written by <b>'onkeyup'</b> event(space,back,return,del), or
<b>'add to backup'</b> button. </p>
<div id="menu">
<a href="#" onclick='BACKUP();return false';>[add to backup]</a>
<a href="#" onclick='setTimeout("undo()",10)'>[undo]</a>
<a href="#" onclick='setTimeout("redo()",10)'>[redo]</a>
<a href="#" onclick='look()'>[view backup-data]</a> </div>
<div id="wisiwig">Type text: </div>
Backup Array Data: <br>
<div id="bup">
<textarea id="bup_a" name="bup_a" cols="80" rows="6"></textarea>
</div>
</body>
</html>