Dominick
01-07-2004, 05:00 PM
Hello and thanks so much in advance for your help.
I am creating a rich text/HTML editor for late model IE only. I have most of the particulars worked out but there's one challenge that I'm stumped on...
The editable area consists of an IFRAME. All the formatting options work OK (text color, bold, etc.) but what I'd like to do is add a "theme" to the message that is being composed - in other words, give the user the option of setting a default background color and font style that will be used throughout the entire message.
What I've done is create a "Theme" dropdown that changes the IFRAME's src value to one of three external HTML files. Each of these HTML files contains the CSS attributes that create the theme (for now it's just background color, font face & color).
This seems to do the trick, and it works quite well with one exception: If a user enters some text and then decides to change the theme, the previously-entered text is lost. Is there any way to change the IFRAME's source file without forcing a reload of the IFRAME's, thereby losing its text content? Should I re-think my overall approach?
Thanks so much in advance...
Here's a link to a demo. (http://www.geocities.com/dominickpagano/editor/test.html)
Here's the code for the file that contains the editor. The code for one of the theme files will follow:
==============================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="javascript" type="text/javascript">
var viewMode = 1;
function doToggleView()
{
if(viewMode == 1)
{
iHTML = edit.document.body.innerHTML;
edit.document.body.innerText = iHTML;
toolbar.style.display = 'none';
edit.focus();
viewMode = 2;
}
else
{
iText = edit.document.body.innerText;
edit.document.body.innerHTML = iText;
toolbar.style.display = 'inline';
edit.focus();
viewMode = 1;
}
}
var rng;
// theme
function work(a)
{
if (a.theme[0].selected)
parent.editArea.location.href = 'theme_00.html';
if (a.theme[1].selected)
parent.editArea.location.href = 'theme_01.html';
if (a.theme[2].selected)
parent.editArea.location.href = 'theme_02.html';
if (a.theme[3].selected)
parent.editArea.location.href = 'theme_03.html';
}
// theme
//Function to format text in the text box
function FormatText(command, option) {
if ((command == "forecolor")||(command == "backcolor")) {
parent.command = command;
buttonElement = document.getElementById(command);
document.getElementById("colorpalette").style.left = getOffsetLeft(buttonElement) + "px";
document.getElementById("colorpalette").style.top = (getOffsetTop(buttonElement) +
buttonElement.offsetHeight) + "px";
if (document.getElementById("colorpalette").style.visibility == "hidden")
document.getElementById("colorpalette").style.visibility="visible";
else {
document.getElementById("colorpalette").style.visibility="hidden";
}
//get current selected range
var sel = document.getElementById('edit').contentWindow.document.selection;
if (sel!=null) {
rng = sel.createRange();
}
}
else {
document.getElementById("edit").contentWindow.focus();
document.getElementById("edit").contentWindow.document.execCommand(command, false, option);
document.getElementById("edit").contentWindow.focus();
}
}
//Function to set color
function setColor(color) {
//retrieve selected range
var sel = document.getElementById('edit').contentWindow.document.selection;
if (sel!=null) {
var newRng = sel.createRange();
newRng = rng;
newRng.select();
}
document.getElementById("edit").contentWindow.document.execCommand(parent.command, false, color);
document.getElementById("edit").contentWindow.focus();
document.getElementById("colorpalette").style.visibility="hidden";
}
function getOffsetTop(elm) {
var mOffsetTop = elm.offsetTop;
var mOffsetParent = elm.offsetParent;
while(mOffsetParent){
mOffsetTop += mOffsetParent.offsetTop;
mOffsetParent = mOffsetParent.offsetParent;
}
return mOffsetTop;
}
function getOffsetLeft(elm) {
var mOffsetLeft = elm.offsetLeft;
var mOffsetParent = elm.offsetParent;
while(mOffsetParent) {
mOffsetLeft += mOffsetParent.offsetLeft;
mOffsetParent = mOffsetParent.offsetParent;
}
return mOffsetLeft;
}
function Select(selectname)
{
var cursel = document.getElementById(selectname).selectedIndex;
// First one is always a label
if (cursel != 0) {
var selected = document.getElementById(selectname).options[cursel].value;
document.getElementById('edit').contentWindow.document.execCommand(selectname, false, selected);
document.getElementById(selectname).selectedIndex = 0;
}
document.getElementById("edit").contentWindow.focus();
}
function Start() {
writeRTE();
}
function writeRTE() {
document.writeln('<table border="0" cellpadding="0" cellspacing="0">');
document.writeln('<form name="rte">');
document.writeln('<tr id="toolbar">');
document.writeln('<td>');
document.writeln('<select name="selectFont" id="fontname" onChange="Select(this.id)">');
document.writeln('<option value="Font" selected>Font Face</option>');
document.writeln('<option value="Arial, Helvetica, sans-serif">Arial</option>');
document.writeln('<option value="Courier New, Courier, mono">Courier</option>');
document.writeln('<option value="Georgia, serif">Georgia</option>');
document.writeln('<option value="Times New Roman, Times, serif">Times</option>');
document.writeln('<option value="Verdana, sans-serif">Verdana</option>');
document.writeln('</select>');
document.writeln(' ');
document.writeln('<select unselectable="on" id="fontsize" onchange="Select(this.id);">');
document.writeln('<option value="Size">Size</option>');
document.writeln('<option value="1">1</option>');
document.writeln('<option value="2">2</option>');
document.writeln('<option value="3">3</option>');
document.writeln('<option value="4">4</option>');
document.writeln('<option value="5">5</option>');
document.writeln('<option value="6">6</option>');
document.writeln('<option value="7">7</option>');
document.writeln('</select>');
document.writeln(' ');
document.writeln('<select name="theme" id="theme" onchange="work(rte)">');
document.writeln('<option value="Theme">Theme</option>');
document.writeln('<option value="1">Theme 01</option>');
document.writeln('<option value="2">Theme 02</option>');
document.writeln('<option value="3">Theme 03</option>');
document.writeln('</select>');
document.writeln(' </td>');
// text color
document.writeln('<td align="center"><div id="forecolor"> <a href="#" onClick="FormatText(\'forecolor\', \'\')">TEXT COLOR</a> </div></td>');
// bg color
document.writeln('<td align="center"><div id="backcolor"> <a href="#" onClick="FormatText(\'backcolor\', \'\')">BGCOLOR</a> </div></td>');
// cut
document.writeln('<td> <a href="#" onClick="FormatText(\'cut\')">CUT</a> ');
// copy
document.writeln('<a href="#" onClick="FormatText(\'copy\')">COPY</a> ');
// paste
document.writeln('<a href="#" onClick="FormatText(\'paste\')">PASTE</a> ');
// bold
document.writeln('<a href="#" onClick="FormatText(\'bold\', \'\')">BOLD</a> ');
// italic
document.writeln('<a href="#" onClick="FormatText(\'italic\', \'\')">ITAL</a> ');
// underline
document.writeln('<a href="#" onClick="FormatText(\'underline\', \'\')">UL</a> </td>');
document.writeln('</tr>');
document.writeln('<tr><td colspan="4" height="3"></td></td>');
document.writeln('</table>');
document.writeln('<iframe id="edit" name="editArea" style="height:220px; width:684px; padding:0;" src="theme_00.html"></iframe>');
document.writeln('<iframe id="colorpalette" src="palette.html" style="visibility:hidden; position: absolute; left: 0px; top: 0px; height:122px; width:166px;"></iframe>');
document.writeln('<iframe src="themes/index.html" name="themelist" id="themelist" style="visibility:hidden; position: absolute; left: 0px; top: 0px; height:235px; width:405px;" scrolling="no"></iframe>');
setTimeout("enableDesignMode()", 10);
document.writeln('</form>');
}
function writeDefault() {
document.writeln('<textarea id="edit" name="_msg" style="width: 678px; height: 220px;"></textarea>');
}
function enableDesignMode() {
frames.edit.document.designMode = "On";
}
</script>
<style>
a {
font-family : Verdana;
font-size : 10px;
}
</style>
</head>
<body>
<script language="javascript" type="text/javascript">
<!--
Start();
//-->
</script>
</body>
</html>
==============================================
Theme:
==============================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<style>
body {
font-family : Verdana;
font-size : inherit;
color : #000000;
}
</style>
</head>
<body bgcolor="#fff8dc">
</body>
</html>
==============================================
I am creating a rich text/HTML editor for late model IE only. I have most of the particulars worked out but there's one challenge that I'm stumped on...
The editable area consists of an IFRAME. All the formatting options work OK (text color, bold, etc.) but what I'd like to do is add a "theme" to the message that is being composed - in other words, give the user the option of setting a default background color and font style that will be used throughout the entire message.
What I've done is create a "Theme" dropdown that changes the IFRAME's src value to one of three external HTML files. Each of these HTML files contains the CSS attributes that create the theme (for now it's just background color, font face & color).
This seems to do the trick, and it works quite well with one exception: If a user enters some text and then decides to change the theme, the previously-entered text is lost. Is there any way to change the IFRAME's source file without forcing a reload of the IFRAME's, thereby losing its text content? Should I re-think my overall approach?
Thanks so much in advance...
Here's a link to a demo. (http://www.geocities.com/dominickpagano/editor/test.html)
Here's the code for the file that contains the editor. The code for one of the theme files will follow:
==============================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="javascript" type="text/javascript">
var viewMode = 1;
function doToggleView()
{
if(viewMode == 1)
{
iHTML = edit.document.body.innerHTML;
edit.document.body.innerText = iHTML;
toolbar.style.display = 'none';
edit.focus();
viewMode = 2;
}
else
{
iText = edit.document.body.innerText;
edit.document.body.innerHTML = iText;
toolbar.style.display = 'inline';
edit.focus();
viewMode = 1;
}
}
var rng;
// theme
function work(a)
{
if (a.theme[0].selected)
parent.editArea.location.href = 'theme_00.html';
if (a.theme[1].selected)
parent.editArea.location.href = 'theme_01.html';
if (a.theme[2].selected)
parent.editArea.location.href = 'theme_02.html';
if (a.theme[3].selected)
parent.editArea.location.href = 'theme_03.html';
}
// theme
//Function to format text in the text box
function FormatText(command, option) {
if ((command == "forecolor")||(command == "backcolor")) {
parent.command = command;
buttonElement = document.getElementById(command);
document.getElementById("colorpalette").style.left = getOffsetLeft(buttonElement) + "px";
document.getElementById("colorpalette").style.top = (getOffsetTop(buttonElement) +
buttonElement.offsetHeight) + "px";
if (document.getElementById("colorpalette").style.visibility == "hidden")
document.getElementById("colorpalette").style.visibility="visible";
else {
document.getElementById("colorpalette").style.visibility="hidden";
}
//get current selected range
var sel = document.getElementById('edit').contentWindow.document.selection;
if (sel!=null) {
rng = sel.createRange();
}
}
else {
document.getElementById("edit").contentWindow.focus();
document.getElementById("edit").contentWindow.document.execCommand(command, false, option);
document.getElementById("edit").contentWindow.focus();
}
}
//Function to set color
function setColor(color) {
//retrieve selected range
var sel = document.getElementById('edit').contentWindow.document.selection;
if (sel!=null) {
var newRng = sel.createRange();
newRng = rng;
newRng.select();
}
document.getElementById("edit").contentWindow.document.execCommand(parent.command, false, color);
document.getElementById("edit").contentWindow.focus();
document.getElementById("colorpalette").style.visibility="hidden";
}
function getOffsetTop(elm) {
var mOffsetTop = elm.offsetTop;
var mOffsetParent = elm.offsetParent;
while(mOffsetParent){
mOffsetTop += mOffsetParent.offsetTop;
mOffsetParent = mOffsetParent.offsetParent;
}
return mOffsetTop;
}
function getOffsetLeft(elm) {
var mOffsetLeft = elm.offsetLeft;
var mOffsetParent = elm.offsetParent;
while(mOffsetParent) {
mOffsetLeft += mOffsetParent.offsetLeft;
mOffsetParent = mOffsetParent.offsetParent;
}
return mOffsetLeft;
}
function Select(selectname)
{
var cursel = document.getElementById(selectname).selectedIndex;
// First one is always a label
if (cursel != 0) {
var selected = document.getElementById(selectname).options[cursel].value;
document.getElementById('edit').contentWindow.document.execCommand(selectname, false, selected);
document.getElementById(selectname).selectedIndex = 0;
}
document.getElementById("edit").contentWindow.focus();
}
function Start() {
writeRTE();
}
function writeRTE() {
document.writeln('<table border="0" cellpadding="0" cellspacing="0">');
document.writeln('<form name="rte">');
document.writeln('<tr id="toolbar">');
document.writeln('<td>');
document.writeln('<select name="selectFont" id="fontname" onChange="Select(this.id)">');
document.writeln('<option value="Font" selected>Font Face</option>');
document.writeln('<option value="Arial, Helvetica, sans-serif">Arial</option>');
document.writeln('<option value="Courier New, Courier, mono">Courier</option>');
document.writeln('<option value="Georgia, serif">Georgia</option>');
document.writeln('<option value="Times New Roman, Times, serif">Times</option>');
document.writeln('<option value="Verdana, sans-serif">Verdana</option>');
document.writeln('</select>');
document.writeln(' ');
document.writeln('<select unselectable="on" id="fontsize" onchange="Select(this.id);">');
document.writeln('<option value="Size">Size</option>');
document.writeln('<option value="1">1</option>');
document.writeln('<option value="2">2</option>');
document.writeln('<option value="3">3</option>');
document.writeln('<option value="4">4</option>');
document.writeln('<option value="5">5</option>');
document.writeln('<option value="6">6</option>');
document.writeln('<option value="7">7</option>');
document.writeln('</select>');
document.writeln(' ');
document.writeln('<select name="theme" id="theme" onchange="work(rte)">');
document.writeln('<option value="Theme">Theme</option>');
document.writeln('<option value="1">Theme 01</option>');
document.writeln('<option value="2">Theme 02</option>');
document.writeln('<option value="3">Theme 03</option>');
document.writeln('</select>');
document.writeln(' </td>');
// text color
document.writeln('<td align="center"><div id="forecolor"> <a href="#" onClick="FormatText(\'forecolor\', \'\')">TEXT COLOR</a> </div></td>');
// bg color
document.writeln('<td align="center"><div id="backcolor"> <a href="#" onClick="FormatText(\'backcolor\', \'\')">BGCOLOR</a> </div></td>');
// cut
document.writeln('<td> <a href="#" onClick="FormatText(\'cut\')">CUT</a> ');
// copy
document.writeln('<a href="#" onClick="FormatText(\'copy\')">COPY</a> ');
// paste
document.writeln('<a href="#" onClick="FormatText(\'paste\')">PASTE</a> ');
// bold
document.writeln('<a href="#" onClick="FormatText(\'bold\', \'\')">BOLD</a> ');
// italic
document.writeln('<a href="#" onClick="FormatText(\'italic\', \'\')">ITAL</a> ');
// underline
document.writeln('<a href="#" onClick="FormatText(\'underline\', \'\')">UL</a> </td>');
document.writeln('</tr>');
document.writeln('<tr><td colspan="4" height="3"></td></td>');
document.writeln('</table>');
document.writeln('<iframe id="edit" name="editArea" style="height:220px; width:684px; padding:0;" src="theme_00.html"></iframe>');
document.writeln('<iframe id="colorpalette" src="palette.html" style="visibility:hidden; position: absolute; left: 0px; top: 0px; height:122px; width:166px;"></iframe>');
document.writeln('<iframe src="themes/index.html" name="themelist" id="themelist" style="visibility:hidden; position: absolute; left: 0px; top: 0px; height:235px; width:405px;" scrolling="no"></iframe>');
setTimeout("enableDesignMode()", 10);
document.writeln('</form>');
}
function writeDefault() {
document.writeln('<textarea id="edit" name="_msg" style="width: 678px; height: 220px;"></textarea>');
}
function enableDesignMode() {
frames.edit.document.designMode = "On";
}
</script>
<style>
a {
font-family : Verdana;
font-size : 10px;
}
</style>
</head>
<body>
<script language="javascript" type="text/javascript">
<!--
Start();
//-->
</script>
</body>
</html>
==============================================
Theme:
==============================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<style>
body {
font-family : Verdana;
font-size : inherit;
color : #000000;
}
</style>
</head>
<body bgcolor="#fff8dc">
</body>
</html>
==============================================