Skip to content

Commit

Permalink
Handle browser that don't do clipboardData. Add compat note to readme
Browse files Browse the repository at this point in the history
Closes jbt#14
  • Loading branch information
jbt committed Oct 17, 2015
1 parent 06cd3c2 commit 4812667
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 41 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ As seen in my [online markdown editor](//jbt.github.io/markdown-editor) (the lef

Requires [Prism](//prismjs.com) - Prism core is required plus any languages you want to be syntax-highlighted inside fenced code blocks. The bundled `prism-all.js` includes _all_ available languages.


## Usage

* Include `prism.css` and `mdedit.css`
* Include `prism-all.js` and `mdedit.js`
* Include a `<pre>` element where you want an editor
* Then `var editor = mdEdit(thatPreElement, {options})`;


## API

* The `options` parameter to the constructor may include the following configuration options:
Expand All @@ -21,3 +23,8 @@ Requires [Prism](//prismjs.com) - Prism core is required plus any languages you

* `editor.getValue()` - returns the current value of the editor view
* `editor.setValue(val)` - sets the current value to `val` and updates the view


## Browser support

Anything that supports ES5 well enough. That means (hopefully) IE9+, and all recent versions of all the other browsers.
41 changes: 21 additions & 20 deletions mdedit.js
Original file line number Diff line number Diff line change
Expand Up @@ -862,10 +862,6 @@ Editor.prototype.keyup = function(evt){
var keyCode = evt && evt.keyCode || 0,
code = this.getText();

// if(keyCode < 9 || keyCode == 13 || keyCode > 32 && keyCode < 41) {
// $t.trigger('caretmove');
// }

if([
9, 91, 93, 16, 17, 18, // modifiers
20, // caps lock
Expand Down Expand Up @@ -901,7 +897,6 @@ Editor.prototype.changed = function(evt){
this._prevHTML = setHTML = Prism['highlight'](code, md);
}
this._prevCode = code;
// Prism.highlightElement(this); // bit messy + unnecessary + strips leading newlines :(

if(setHTML !== undefined){
if(!/\n$/.test(code)) {
Expand Down Expand Up @@ -1016,13 +1011,6 @@ Editor.prototype.keydown = function(evt){
evt.preventDefault();
}

break;
case 191:
// if(cmdOrCtrl && !evt.altKey) {
// that.action('comment', { lang: this.id });
// return false;
// }

break;
}
};
Expand Down Expand Up @@ -1079,6 +1067,20 @@ Editor.prototype.paste = function(evt){
var end = this.selMgr.getEnd();
var selection = start === end ? '' : this.getText().slice(start, end);

var self = this;

function applyPasted(pasted){
self.undoMgr.action({
add: pasted,
del: selection,
start: start
});

start += pasted.length;
self.selMgr.setRange(start, start);
self.changed();
}

if(evt.clipboardData){
evt.preventDefault();

Expand All @@ -1090,15 +1092,14 @@ Editor.prototype.paste = function(evt){
start: start
});

this.undoMgr.action({
add: pasted,
del: selection,
start: start
});
applyPasted(pasted);
}else{
// handle IE9 with no clipboardData. Flickers a bit if styles have changed :(
setTimeout(function(){
var newEnd = self.selMgr.getEnd();

start += pasted.length;
this.selMgr.setRange(start, start);
this.changed();
applyPasted(self.getText().slice(start, newEnd));
}, 0);
}
};

Expand Down
2 changes: 1 addition & 1 deletion mdedit.min.js

Large diffs are not rendered by default.

41 changes: 21 additions & 20 deletions src/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ Editor.prototype.keyup = function(evt){
var keyCode = evt && evt.keyCode || 0,
code = this.getText();

// if(keyCode < 9 || keyCode == 13 || keyCode > 32 && keyCode < 41) {
// $t.trigger('caretmove');
// }

if([
9, 91, 93, 16, 17, 18, // modifiers
20, // caps lock
Expand Down Expand Up @@ -110,7 +106,6 @@ Editor.prototype.changed = function(evt){
this._prevHTML = setHTML = Prism['highlight'](code, md);
}
this._prevCode = code;
// Prism.highlightElement(this); // bit messy + unnecessary + strips leading newlines :(

if(setHTML !== undefined){
if(!/\n$/.test(code)) {
Expand Down Expand Up @@ -225,13 +220,6 @@ Editor.prototype.keydown = function(evt){
evt.preventDefault();
}

break;
case 191:
// if(cmdOrCtrl && !evt.altKey) {
// that.action('comment', { lang: this.id });
// return false;
// }

break;
}
};
Expand Down Expand Up @@ -288,6 +276,20 @@ Editor.prototype.paste = function(evt){
var end = this.selMgr.getEnd();
var selection = start === end ? '' : this.getText().slice(start, end);

var self = this;

function applyPasted(pasted){
self.undoMgr.action({
add: pasted,
del: selection,
start: start
});

start += pasted.length;
self.selMgr.setRange(start, start);
self.changed();
}

if(evt.clipboardData){
evt.preventDefault();

Expand All @@ -299,14 +301,13 @@ Editor.prototype.paste = function(evt){
start: start
});

this.undoMgr.action({
add: pasted,
del: selection,
start: start
});
applyPasted(pasted);
}else{
// handle IE9 with no clipboardData. Flickers a bit if styles have changed :(
setTimeout(function(){
var newEnd = self.selMgr.getEnd();

start += pasted.length;
this.selMgr.setRange(start, start);
this.changed();
applyPasted(self.getText().slice(start, newEnd));
}, 0);
}
};

0 comments on commit 4812667

Please sign in to comment.