Skip to content

Commit

Permalink
importManager added fixed #300, #304, #305, #312
Browse files Browse the repository at this point in the history
  • Loading branch information
shiren authored and seonim-ryu committed Jan 2, 2020
1 parent 437ec4b commit 084be67
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 62 deletions.
9 changes: 0 additions & 9 deletions apps/core/demo/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,6 @@
$form[0].submit();
//callback으로 url전달
//callback('이미지URL');
},
'addImageBlobHook': function(blob, callback) {
var reader = new FileReader();

reader.onload = function(event){
callback(event.target.result);
};

reader.readAsDataURL(blob);
}
}
});
Expand Down
49 changes: 3 additions & 46 deletions apps/core/src/js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var MarkdownEditor = require('./markdownEditor'),
EventManager = require('./eventManager'),
CommandManager = require('./commandManager'),
extManager = require('./extManager'),
ImportManager = require('./importManager'),
Convertor = require('./convertor');

//markdown commands
Expand Down Expand Up @@ -77,6 +78,8 @@ function NeonEditor(options) {

this.eventManager = new EventManager();

this.importManager = new ImportManager(this.eventManager);

this.commandManager = new CommandManager(this);
this.convertor = new Convertor(this.eventManager);

Expand Down Expand Up @@ -119,8 +122,6 @@ function NeonEditor(options) {
self.eventManager.emit('load', self);
});

this._initImportEvent();

__nedInstance.push(this);
}

Expand Down Expand Up @@ -149,50 +150,6 @@ NeonEditor.prototype._initDefaultCommands = function() {
this.commandManager.addCommand(wwTask);
};

NeonEditor.prototype._initImportEvent = function() {
var self = this;

this.eventManager.listen('paste', function(ev) {
var items, blob, i, itemLen;

items = ev.data.clipboardData && ev.data.clipboardData.items;

if (items) {
itemLen = items.length;

for (i = 0; i < itemLen; i += 1) {
if (items[i].type.indexOf('image') !== -1) {
blob = items[i].getAsFile();

self.eventManager.emit('addImageBlobHook', blob, function(url) {
self.eventManager.emit('command', 'AddImage', {imageUrl: url, altText: blob.name || 'image'});
});
}
}
}
});

this.eventManager.listen('drop', function(ev) {
var items, blob, i, itemLen;

items = ev.data.dataTransfer && ev.data.dataTransfer.files;

if (items) {
itemLen = items.length;

for (i = 0; i < itemLen; i += 1) {
if (items[i].type.indexOf('image') !== -1) {
blob = items[i];

self.eventManager.emit('addImageBlobHook', blob, function(url) {
self.eventManager.emit('command', 'AddImage', {imageUrl: url, altText: blob.name || 'image'});
});
}
}
}
});
};

/**
* 프리뷰가 보여지는 방식을 변경한다
* @param {string} style 스타일 이름 tab, vertical
Expand Down
78 changes: 78 additions & 0 deletions apps/core/src/js/importManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* @fileoverview Implement Module for managing import external data such as image
* @author Sungho Kim(sungho-kim@nhnent.com) FE Development Team/NHN Ent.
*/

'use strict';

var util = ne.util;

/**
* ImportManager
* @exports ImportManager
* @constructor
* @class
* @param {EventManager} eventManager eventManager
*/
function ImportManager(eventManager) {
this.eventManager = eventManager;

this._initDropEvent();
this._initPasteEvent();
this._initDefaultImporter();
}

ImportManager.prototype._initDropEvent = function() {
var self = this;

this.eventManager.listen('drop', function(ev) {
var items = ev.data.dataTransfer && ev.data.dataTransfer.files;
self._forEachItems(items);
});
};

ImportManager.prototype._initPasteEvent = function() {
var self = this;

this.eventManager.listen('paste', function(ev) {
var items = ev.data.clipboardData && ev.data.clipboardData.items;
self._forEachItems(items);
});
};

ImportManager.prototype._initDefaultImporter = function() {
this.eventManager.listen('addImageBlobHook', function(blob, callback) {
var reader = new FileReader();

reader.onload = function(event) {
callback(event.target.result);
};

reader.readAsDataURL(blob);
});
};

ImportManager.prototype._emitAddImageBlobHook = function(item) {
var self = this,
blob = item.name ? item : item.getAsFile(); //Blob or File

console.log('dd');

this.eventManager.emit('addImageBlobHook', blob, function(url) {
self.eventManager.emit('command', 'AddImage', {imageUrl: url, altText: blob.name || 'image'});
});
};

ImportManager.prototype._forEachItems = function(items) {
var self = this;

if (items) {
util.forEachArray(items, function(item) {
if (item.type.indexOf('image') !== -1) {
self._emitAddImageBlobHook(item);
}
});
}
};

module.exports = ImportManager;
2 changes: 2 additions & 0 deletions apps/core/src/js/markdownEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ MarkdownEditor.prototype._initEvent = function() {
});

this.cm.on('drop', function(cm, eventData) {
eventData.preventDefault();

self.eventManager.emit('drop', {
source: 'markdown',
data: eventData
Expand Down
7 changes: 0 additions & 7 deletions apps/core/src/js/wysiwygEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,6 @@ WysiwygEditor.prototype._initSquireEvent = function() {
source: 'wysiwyg'
});
});

this.getEditor().addEventListener('paste', function(clipboardEvent) {
self.eventManager.emit('paste', {
source: 'wysiwyg',
clipboardEvent: clipboardEvent
});
});
};

WysiwygEditor.prototype._keyEventHandler = function(event) {
Expand Down

0 comments on commit 084be67

Please sign in to comment.