| author | Bogdan Gheorghe | 2013-02-04 13:14:59 (EST) |
|---|---|---|
| committer | Silenio Quarti | 2013-02-04 13:15:29 (EST) |
| commit | be95ea8fd435f4f4e51de07db0e6de6872d3318d (patch) (side-by-side diff) | |
| tree | f73a7b41cde51c54c06def9249667952d65765c9 | |
| parent | 52f271066429b9cda5cc64f3bbc25effc08fd720 (diff) | |
| download | org.eclipse.orion.client-be95ea8fd435f4f4e51de07db0e6de6872d3318d.zip org.eclipse.orion.client-be95ea8fd435f4f4e51de07db0e6de6872d3318d.tar.gz org.eclipse.orion.client-be95ea8fd435f4f4e51de07db0e6de6872d3318d.tar.bz2 | |
Bug 399916 - Editor#edit function should be able to get the contents of the parent DOM node
| -rw-r--r-- | bundles/org.eclipse.orion.client.editor/web/examples/editor/editSample.html | 46 | ||||
| -rw-r--r-- | bundles/org.eclipse.orion.client.editor/web/orion/editor/edit.js | 35 |
2 files changed, 76 insertions, 5 deletions
diff --git a/bundles/org.eclipse.orion.client.editor/web/examples/editor/editSample.html b/bundles/org.eclipse.orion.client.editor/web/examples/editor/editSample.html new file mode 100644 index 0000000..080f71f --- a/dev/null +++ b/bundles/org.eclipse.orion.client.editor/web/examples/editor/editSample.html @@ -0,0 +1,46 @@ +<!DOCTYPE html> +<!-- + Sample that shows how to use the edit.js file to create an editor. For a complete list + of editor configuration options, see orion.editor#edit. +--> +<html> +<head> +<link rel="stylesheet" type="text/css" href="themes/default.css"/> +<style> +#editor { + border: 1px solid teal; + position: absolute; + top: 0px; + left: 0px; + bottom: 0px; + right: 0px; + margin: 20px; +} + +pre { + margin-top: 0px; +} +</style> +<!-- Note if running this standalone you will need to copy requirejs from bundles/org.eclipse.orion.client.core/web/ --> +<script src="../../requirejs/require.js"></script> +<script type="text/javascript"> + /*globals require*/ + require({ + baseUrl: '../..', + paths: { + i18n: 'requirejs/i18n' + } + }); + require(["orion/editor/edit"], function(edit) { + edit({ + lang: "js" + }); + }); +</script> +</head> +<body> +<div id="editor"><pre>function () { + var a = 'hi there!'; +}</pre></div> +</body> +</html> diff --git a/bundles/org.eclipse.orion.client.editor/web/orion/editor/edit.js b/bundles/org.eclipse.orion.client.editor/web/orion/editor/edit.js index e81a013..5a21092 100644 --- a/bundles/org.eclipse.orion.client.editor/web/orion/editor/edit.js +++ b/bundles/org.eclipse.orion.client.editor/web/orion/editor/edit.js @@ -40,6 +40,28 @@ define('orion/editor/edit', [ "examples/editor/textStyler" ], function(mTextView, mTextModel, mProjModel, mEventTarget, mKeyBinding, mRulers, mAnnotations, mTooltip, mUndoStack, mTextDND, mEditor, mEditorFeatures, mContentAssist, mCSSContentAssist, mHtmlContentAssist, mJSContentAssist, mAsyncStyler, mMirror, mTextMateStyler, mHtmlGrammar, mTextStyler) { + /** @private */ + function getTextFromElement(element) { + if (!window.getSelection) { + return element.innerText || element.textContent; + } + var newRange = document.createRange(); + newRange.selectNode(element); + var selection = window.getSelection(); + var oldRanges = [], i; + for (i = 0; i < selection.rangeCount; i++) { + oldRanges.push(selection.getRangeAt(i)); + } + selection.removeAllRanges(); + selection.addRange(newRange); + var text = selection.toString(); + selection.removeAllRanges(); + for (i = 0; i < oldRanges.length; i++) { + selection.addRange(oldRanges[i]); + } + return text; + } + /** * @class This object describes the options for <code>edit</code>. * @name orion.editor.EditOptions @@ -141,12 +163,15 @@ define('orion/editor/edit', [ statusReporter: options.statusReporter, domNode: parent }); - - editor.installTextView(); - // if there is a mechanism to change which file is being viewed, this code would be run each time it changed. - if (options.contents) { - editor.setInput(options.title, null, options.contents); + + var contents = options.contents; + if (contents === undefined) { + contents = getTextFromElement(parent); } + if (!contents) { contents=""; } + + editor.installTextView(); + editor.setInput(options.title, null, contents); syntaxHighlighter.highlight(options.lang, editor); contentAssist.addEventListener("Activating", function() { if (/\.css$/.test(options.lang)) { |

