Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions demo.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
var jsEdit = require('./')({ container: document.querySelector('#editor') })
jsEdit.setValue(document.querySelector('#program').text)
var jsEdit = require('./')({
container: document.querySelector('#editor'),
injectStyles: true,
})

jsEdit.setValue(document.querySelector('#program').text)
3 changes: 0 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
<html>
<head>
<title>javascript-editor</title>
<link rel="stylesheet" href="css/codemirror.css">
<link rel="stylesheet" href="css/theme.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id='editor'>
Expand Down
38 changes: 34 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var path = require('path')
var events = require('events')
var inherits = require('inherits')
var extend = require('extend')
Expand All @@ -20,6 +21,7 @@ function Editor(opts) {
var defaults = {
value: "// hello world\n",
mode: "javascript",
theme: 'mistakes',
lineNumbers: true,
autofocus: (window === window.top),
matchBrackets: true,
Expand All @@ -28,11 +30,12 @@ function Editor(opts) {
tabSize: 2,
indentUnit: 2,
updateInterval: 500,
dragAndDrop: true
dragAndDrop: true,
injectStyles: false
}
this.opts = extend({}, defaults, opts)
this.editor = CodeMirror( this.opts.container, this.opts )
this.editor.setOption("theme", "mistakes") // borrowed from mistakes.io
this.editor.setOption("theme", opts.theme)
this.editor.setCursor(this.editor.lineCount(), 0)
this.editor.on('change', function (e) {
self.emit('change')
Expand All @@ -47,16 +50,43 @@ function Editor(opts) {
tabSize: 2,
readOnly: 'nocursor'
})
this.results.setOption("theme", 'mistakes')
this.results.setOption("theme", opts.theme)
}
this.update()
if (this.opts.dragAndDrop) this.addDropHandler()
if (this.opts.injectStyles) {
// required for callback
window.__jsEditor = this;
injectStyles()
}
}

inherits(Editor, events.EventEmitter)

function injectStyles() {
['./css/codemirror.css','./css/style.css','./css/theme.css'].forEach(function(cssFile) {
var pathname = path.join(__dirname,cssFile)
injectStyle( pathname )
})
}

function injectStyle( pathname ) {
var fileref = document.createElement("link")
fileref.setAttribute("rel", "stylesheet")
fileref.setAttribute("type", "text/css")
fileref.setAttribute("href", pathname)
fileref.setAttribute("onload", "__jsEditor.refresh()")
document.getElementsByTagName("head")[0].appendChild(fileref)
}

Editor.prototype.refresh = function() {
this.editor.refresh()
}

Editor.prototype.update = function() {
return this.validate(this.editor.getValue())
var hasErrors = this.validate(this.editor.getValue())
this.emit('valid', hasErrors)
return hasErrors
}

Editor.prototype.validate = function(value) {
Expand Down
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "javascript-editor",
"version": "0.1.0",
"version": "0.2.0",
"description": "codemirror + esprima powered html5 javascript editor component",
"repository": {
"type": "git",
Expand All @@ -10,16 +10,13 @@
"extend": "1.1.3",
"inherits": "1.0.0",
"esprima": "1.0.2",
"codemirror": "https://github.com/tmcw/CodeMirror/archive/module-export.tar.gz"
"codemirror": "^5.11.0"
},
"devDependencies": {
"browserify": "2.4.3",
"browservefy": "0.0.9"
},
"scripts": {
"start": "./node_modules/browservefy/bin/browservefy demo.js:bundle.js 8080 --browserify=./node_modules/browserify/bin/cmd.js -- -d"
},
"engines": {
"node": "0.8.x"
}
}
10 changes: 9 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ var editor = createEditor({ container: document.querySelector('#editor') })
editor.on('change', function() {
var value = editor.getValue()
})

editor.on('valid', function(noErrors) {
// noErrors is a boolean
})
```

### default options
Expand All @@ -42,10 +46,14 @@ var defaults = {
tabSize: 2,
indentUnit: 2,
updateInterval: 500,
dragAndDrop: true
dragAndDrop: true,
injectStyles: false,
}
```

### inject styles
if you want to use the default styles automatically, use the option `injectStyles: true`, or manually include them yourself

## license

BSD
Loading