| 1 | /* |
|---|
| 2 | $Id$ |
|---|
| 3 | |
|---|
| 4 | Copyright Six Apart, Ltd. All rights reserved. |
|---|
| 5 | Redistribution and use in source and binary forms is |
|---|
| 6 | subject to the Six Apart JavaScript license: |
|---|
| 7 | |
|---|
| 8 | http://code.sixapart.com/svn/js/trunk/LICENSE.txt |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | Editor.Textarea = new Class( Component, { |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | initObject: function( element, editor ) { |
|---|
| 16 | arguments.callee.applySuper( this, arguments ); |
|---|
| 17 | this.editor = editor; |
|---|
| 18 | this.range = null; |
|---|
| 19 | }, |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | destroyObject: function() { |
|---|
| 23 | this.range = null; |
|---|
| 24 | this.editor = null; |
|---|
| 25 | arguments.callee.applySuper( this, arguments ); |
|---|
| 26 | }, |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | eventKeyDown: function( event ) { |
|---|
| 30 | this.editor.setChanged(); |
|---|
| 31 | }, |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | getHTML: function() { |
|---|
| 35 | var selection = this.getSelection(); |
|---|
| 36 | if ( selection.createRange ) { |
|---|
| 37 | this.focus(); |
|---|
| 38 | this.range = selection.createRange().duplicate(); |
|---|
| 39 | } |
|---|
| 40 | return this.element.value; |
|---|
| 41 | }, |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | setHTML: function( html ) { |
|---|
| 45 | this.element.value = html; |
|---|
| 46 | }, |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | insertHTML: function( html, select, id, isTempId ) { |
|---|
| 50 | this.setSelection( html ); |
|---|
| 51 | }, |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | focus: function() { |
|---|
| 55 | return this.element.focus(); |
|---|
| 56 | }, |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | execCommand: function( command, userInterface, argument ) { |
|---|
| 60 | /* Possible commands: |
|---|
| 61 | * fontSizeSmaller - not supported |
|---|
| 62 | * fontSizeLarger - not supported |
|---|
| 63 | * - |
|---|
| 64 | * bold |
|---|
| 65 | * italic |
|---|
| 66 | * underline |
|---|
| 67 | * strikethrough |
|---|
| 68 | * - |
|---|
| 69 | * createLink |
|---|
| 70 | * - |
|---|
| 71 | * indent |
|---|
| 72 | * outdent - not supported |
|---|
| 73 | * - |
|---|
| 74 | * insertUnorderedList |
|---|
| 75 | * insertOrderedList |
|---|
| 76 | * - |
|---|
| 77 | * justifyLeft |
|---|
| 78 | * justifyCenter |
|---|
| 79 | * justifyRight |
|---|
| 80 | * - |
|---|
| 81 | * XXX others? |
|---|
| 82 | */ |
|---|
| 83 | var text = this.getSelectedText(); |
|---|
| 84 | if ( !defined( text ) ) |
|---|
| 85 | text = ''; |
|---|
| 86 | switch ( command ) { |
|---|
| 87 | |
|---|
| 88 | case "bold": |
|---|
| 89 | this.setSelection( "<strong>" + text + "</strong>" ); |
|---|
| 90 | break; |
|---|
| 91 | |
|---|
| 92 | case "italic": |
|---|
| 93 | this.setSelection( "<em>" + text + "</em>" ); |
|---|
| 94 | break; |
|---|
| 95 | |
|---|
| 96 | case "underline": |
|---|
| 97 | this.setSelection( "<u>" + text + "</u>" ); |
|---|
| 98 | break; |
|---|
| 99 | |
|---|
| 100 | case "strikethrough": |
|---|
| 101 | this.setSelection( "<strike>" + text + "</strike>" ); |
|---|
| 102 | break; |
|---|
| 103 | |
|---|
| 104 | case "createLink": |
|---|
| 105 | /* XXX escape() argument? */ |
|---|
| 106 | this.setSelection( '<a href="' + argument + '">' + text + "</a>" ); |
|---|
| 107 | break; |
|---|
| 108 | |
|---|
| 109 | case "indent": |
|---|
| 110 | this.setSelection( "<blockquote>" + text + "</blockquote>" ); |
|---|
| 111 | break; |
|---|
| 112 | |
|---|
| 113 | case "insertUnorderedList": |
|---|
| 114 | case "insertOrderedList": |
|---|
| 115 | var list = text.split( /\r?\n/ ); |
|---|
| 116 | var li = []; |
|---|
| 117 | for ( var i = 0; i < list.length; i++ ) |
|---|
| 118 | list[ i ] = "\t<li>" + list[ i ] + "</li>"; |
|---|
| 119 | if ( command == "insertUnorderedList" ) |
|---|
| 120 | this.setSelection( "<ul>\n" + list.join( "\n" ) + "\n</ul>" ); |
|---|
| 121 | else |
|---|
| 122 | this.setSelection( "<ol>\n" + list.join( "\n" ) + "\n</ol>" ); |
|---|
| 123 | break; |
|---|
| 124 | |
|---|
| 125 | case "justifyLeft": |
|---|
| 126 | this.setSelection( '<div style="text-align: left;">' + text + "</div>" ); |
|---|
| 127 | break; |
|---|
| 128 | |
|---|
| 129 | case "justifyCenter": |
|---|
| 130 | this.setSelection( '<div style="text-align: center;">' + text + "</div>" ); |
|---|
| 131 | break; |
|---|
| 132 | |
|---|
| 133 | case "justifyRight": |
|---|
| 134 | this.setSelection( '<div style="text-align: right;">' + text + "</div>" ); |
|---|
| 135 | break; |
|---|
| 136 | |
|---|
| 137 | } |
|---|
| 138 | this.editor.setChanged(); |
|---|
| 139 | }, |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | getSelection: function() { |
|---|
| 143 | return DOM.getSelection( this.window, this.document ); |
|---|
| 144 | }, |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | getSelectedText: function() { |
|---|
| 148 | var selection = this.getSelection(); |
|---|
| 149 | if ( selection.createRange ) { |
|---|
| 150 | // ie |
|---|
| 151 | this.range = null; |
|---|
| 152 | this.focus(); |
|---|
| 153 | var range = selection.createRange(); |
|---|
| 154 | return range.text; |
|---|
| 155 | } else { |
|---|
| 156 | var length = this.element.textLength; |
|---|
| 157 | var start = this.element.selectionStart; |
|---|
| 158 | var end = this.element.selectionEnd; |
|---|
| 159 | if ( end == 1 || end == 2 && defined( length ) ) |
|---|
| 160 | end = length; |
|---|
| 161 | return this.element.value.substring( start, end ); |
|---|
| 162 | } |
|---|
| 163 | }, |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | setSelection: function( txt ) { |
|---|
| 167 | var el = this.element; |
|---|
| 168 | var selection = this.getSelection(); |
|---|
| 169 | if ( selection.createRange ) { |
|---|
| 170 | var range = this.range; |
|---|
| 171 | if ( !range ) { |
|---|
| 172 | this.focus(); |
|---|
| 173 | range = selection.createRange(); |
|---|
| 174 | } |
|---|
| 175 | range.text = txt; |
|---|
| 176 | range.select(); |
|---|
| 177 | } else { |
|---|
| 178 | var scrollTop = el.scrollTop; |
|---|
| 179 | var length = el.textLength; |
|---|
| 180 | var start = el.selectionStart; |
|---|
| 181 | var end = el.selectionEnd; |
|---|
| 182 | if ( end == 1 || end == 2 && defined( length ) ) |
|---|
| 183 | end = length; |
|---|
| 184 | el.value = el.value.substring( 0, start ) + txt + el.value.substr( end, length ); |
|---|
| 185 | el.selectionStart = start; |
|---|
| 186 | el.selectionEnd = start + txt.length; |
|---|
| 187 | el.scrollTop = scrollTop; |
|---|
| 188 | } |
|---|
| 189 | this.focus(); |
|---|
| 190 | }, |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | isTextSelected: function() { |
|---|
| 194 | return true; /* XXX verify */ |
|---|
| 195 | }, |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | getSelectedLink: Function.stub |
|---|
| 199 | |
|---|
| 200 | } ); |
|---|