Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update insert image action: support uploading local image #132

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
node_modules
.vscode
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Included actions:
- Code
- Horizontal Rule
- Link
- Image
- Image (support uploading)

Other available actions (listed at https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand):
- Justify Center
Expand Down
5 changes: 5 additions & 0 deletions demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ <h3>HTML output:</h3>
onChange: function (html) {
document.getElementById('text-output').innerHTML = html
document.getElementById('html-output').textContent = html
},
// if you want to upload image to server
// you must config this
upload: {
api: 'http://localhost:53595/api/files/img/upload'
}
})
</script>
Expand Down
64 changes: 60 additions & 4 deletions dist/pell.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,63 @@ var defaultActions = {
icon: '&#128247;',
title: 'Image',
result: function result() {
var url = window.prompt('Enter the image URL');
if (url) exec('insertImage', url);
// const url = window.prompt('Enter the image URL')
// if (url) exec('insertImage', url)
execInsertImageAction();
}
}

// default for not set `upload` config
};var execInsertImageAction = function execInsertImageAction() {
var uploadImageInput = document.querySelector('.pell input[type="file"]');
if (!uploadImageInput) {
var url = window.prompt('Enter the image URL');
if (url) exec('insertImage', url);
} else {
uploadImageInput.click();
}
};

// just set `url`, `method` and `body` for fetch api
var uploadImage = function uploadImage(_ref, success, error) {
var api = _ref.api,
data = _ref.data;

window.fetch && window.fetch(api, {
method: 'POST',
body: data
}).then(function (res) {
return res.json();
}).then(function (data) {
// responsive data format:
// { success: true, url: 'xxx' }
if (data.success) success(data.url);
}, function (err) {
return error(err);
});
};

var initUploadImageInput = function initUploadImageInput(settings) {
var uploadAPI = settings.upload && settings.upload.api;
if (uploadAPI) {
var input = createElement('input');
input.type = 'file';
input.hidden = true;
addEventListener(input, 'change', function (e) {
var image = e.target.files[0];
var fd = new window.FormData();
fd.append('pell-upload-image', image);
uploadImage({
api: uploadAPI,
data: fd
}, function (url) {
return exec('insertImage', url);
}, function (err) {
return window.alert(err);
});
});
appendChild(settings.element, input);
}
};

var defaultClasses = {
Expand Down Expand Up @@ -170,8 +223,8 @@ var init = function init(settings) {
var content = settings.element.content = createElement('div');
content.contentEditable = true;
content.className = classes.content;
content.oninput = function (_ref) {
var firstChild = _ref.target.firstChild;
content.oninput = function (_ref2) {
var firstChild = _ref2.target.firstChild;

if (firstChild && firstChild.nodeType === 3) exec(formatBlock, '<' + defaultParagraphSeparator + '>');else if (content.innerHTML === '<br>') content.innerHTML = '';
settings.onChange(content.innerHTML);
Expand Down Expand Up @@ -212,6 +265,9 @@ var init = function init(settings) {
if (settings.styleWithCSS) exec('styleWithCSS');
exec(defaultParagraphSeparatorString, defaultParagraphSeparator);

// init a upload image input or not
initUploadImageInput(settings);

return settings.element;
};

Expand Down
2 changes: 1 addition & 1 deletion dist/pell.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 60 additions & 2 deletions src/pell.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,67 @@ const defaultActions = {
icon: '&#128247;',
title: 'Image',
result: () => {
const url = window.prompt('Enter the image URL')
if (url) exec('insertImage', url)
// const url = window.prompt('Enter the image URL')
// if (url) exec('insertImage', url)
execInsertImageAction()
}
}
}

// default for not set `upload` config
const execInsertImageAction = function () {
const uploadImageInput = document.querySelector('.pell input[type="file"]')
if (!uploadImageInput) {
const url = window.prompt('Enter the image URL')
if (url) exec('insertImage', url)
} else {
uploadImageInput.click()
}
}

// just set `url`, `method` and `body` for fetch api
const uploadImage = function ({ api, data }, success, error) {
window.fetch && window.fetch(
api,
{
method: 'POST',
body: data
}
)
.then(res => res.json())
.then(
data => {
// responsive data format:
// { success: true, url: 'xxx' }
if (data.success) success(data.url)
},
err => error(err)
)
}

const initUploadImageInput = function (settings) {
const uploadAPI = settings.upload && settings.upload.api
if (uploadAPI) {
const input = createElement('input')
input.type = 'file'
input.hidden = true
addEventListener(input, 'change', e => {
const image = e.target.files[0]
const fd = new window.FormData()
fd.append('pell-upload-image', image)
uploadImage(
{
api: uploadAPI,
data: fd
},
url => exec('insertImage', url),
err => window.alert(err)
)
})
appendChild(settings.element, input)
}
}

const defaultClasses = {
actionbar: 'pell-actionbar',
button: 'pell-button',
Expand Down Expand Up @@ -155,6 +210,9 @@ export const init = settings => {
if (settings.styleWithCSS) exec('styleWithCSS')
exec(defaultParagraphSeparatorString, defaultParagraphSeparator)

// init a upload image input or not
initUploadImageInput(settings)

return settings.element
}

Expand Down
2 changes: 1 addition & 1 deletion src/pell.scss
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ $pell-content-padding: 10px !default;

.pell-button-selected {
background-color: $pell-button-selected-color;
}
}