Added chrome paste-image-upload. Closes #20.

This commit is contained in:
Dan Brown 2015-10-14 21:46:18 +01:00
parent 1520bde191
commit 7039695b65
6 changed files with 67 additions and 10 deletions

View File

@ -7,7 +7,6 @@ use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Intervention\Image\Facades\Image as ImageTool;
use Illuminate\Support\Facades\DB;
use BookStack\Http\Requests;
use BookStack\Image;
use BookStack\Repos\PageRepo;
@ -37,13 +36,13 @@ class ImageController extends Controller
public function getAll($page = 0)
{
$pageSize = 30;
$images = DB::table('images')->orderBy('created_at', 'desc')
$images = $this->image->orderBy('created_at', 'desc')
->skip($page * $pageSize)->take($pageSize)->get();
foreach ($images as $image) {
$image->thumbnail = $this->getThumbnail($image, 150, 150);
}
$hasMore = count(DB::table('images')->orderBy('created_at', 'desc')
->skip(($page + 1) * $pageSize)->take($pageSize)->get()) > 0;
$hasMore = $this->image->orderBy('created_at', 'desc')
->skip(($page + 1) * $pageSize)->take($pageSize)->count() > 0;
return response()->json([
'images' => $images,
'hasMore' => $hasMore
@ -105,8 +104,8 @@ class ImageController extends Controller
// Create and save image object
$this->image->name = $name;
$this->image->url = $imagePath . $storageName;
$this->image->created_by = Auth::user()->id;
$this->image->updated_by = Auth::user()->id;
$this->image->created_by = auth()->user()->id;
$this->image->updated_by = auth()->user()->id;
$this->image->save();
$this->image->thumbnail = $this->getThumbnail($this->image, 150, 150);
return response()->json($this->image);

BIN
public/loading.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View File

@ -9,11 +9,13 @@ module.exports = {
relative_urls: false,
statusbar: false,
menubar: false,
paste_data_images: false,
//height: 700,
extended_valid_elements: 'pre[*]',
automatic_uploads: false,
valid_children: "-div[p|pre|h1|h2|h3|h4|h5|h6|blockquote]",
plugins: "image table textcolor paste link imagetools fullscreen code hr",
toolbar: "code undo | styleselect | hr bold italic underline strikethrough superscript subscript | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | table image link | fullscreen",
toolbar: "undo redo | styleselect | bold italic underline strikethrough superscript subscript | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | table image link hr | code fullscreen",
content_style: "body {padding-left: 15px !important; padding-right: 15px !important; margin:0!important; margin-left:auto!important;margin-right:auto!important;}",
style_formats: [
{title: "Header 1", format: "h1"},
@ -22,7 +24,8 @@ module.exports = {
{title: "Header 4", format: "h4"},
{title: "Paragraph", format: "p"},
{title: "Blockquote", format: "blockquote"},
{title: "Code Block", icon: "code", format: "pre"}
{title: "Code Block", icon: "code", format: "pre"},
{title: "Inline Code", icon: "code", inline: "code"}
],
file_browser_callback: function(field_name, url, type, win) {
ImageManager.show(function(image) {
@ -35,5 +38,60 @@ module.exports = {
win.document.getElementById(field_name).fireEvent("onchange");
}
});
},
paste_preprocess: function(plugin, args) {
var content = args.content;
if(content.indexOf('<img src="file://') !== -1) {
args.content = '';
}
},
setup: function(editor) {
// Paste image-uploads
editor.on('paste', function(e) {
if(e.clipboardData) {
var items = e.clipboardData.items;
if (items){
for (var i = 0; i < items.length; i++) {
if (items[i].type.indexOf("image") !== -1) {
var file = items[i].getAsFile();
var formData = new FormData();
var ext = 'png';
var xhr = new XMLHttpRequest();
if (file.name) {
var fileNameMatches = file.name.match(/\.(.+)$/);
if (fileNameMatches) {
ext = fileNameMatches[1];
}
}
var id = "image-" + Math.random().toString(16).slice(2);
editor.execCommand('mceInsertContent', false, '<img src="/loading.gif" id="'+id+'">');
var remoteFilename = "image-" + Date.now() + "." + ext;
formData.append('file', file, remoteFilename);
formData.append('_token', document.querySelector('meta[name="token"]').getAttribute('content'));
xhr.open('POST', '/upload/image');
xhr.onload = function() {
if (xhr.status === 200 || xhr.status === 201) {
var result = JSON.parse(xhr.responseText);
//var newImage = editor.getDoc().getElementById(id);
//newImage.setAttribute('src', result.url);
editor.dom.setAttrib(id, 'src', result.url);
console.log(result);
} else {
console.log('An error occured uploading the image');
console.log(xhr.responseText);
}
};
xhr.send(formData);
}
}
}
}
});
}
};

View File

@ -16,4 +16,5 @@
@endif
</form>
</div>
<image-manager></image-manager>
@stop

View File

@ -14,5 +14,6 @@
@include('pages/form', ['model' => $page])
</form>
</div>
<image-manager></image-manager>
@stop

View File

@ -35,5 +35,3 @@
@endif
</div>
</div>
<image-manager></image-manager>