2018-01-28 23:22:43 +11:00
|
|
|
//ドラッグ・アンド・ドロップからアップロードまで。uiのimg.jsとは異なります。
|
|
|
|
var obj = $("body");
|
|
|
|
var system;
|
|
|
|
//ドラッグスタート
|
|
|
|
obj.on('dragstart', function(e) {
|
|
|
|
system = "locked"
|
|
|
|
});
|
|
|
|
//何もなくファイルが通過
|
|
|
|
obj.on('dragend', function(e) {
|
|
|
|
system = "";
|
|
|
|
});
|
|
|
|
//ドラッグファイルが画面上に
|
|
|
|
obj.on('dragenter', function(e) {
|
|
|
|
if (system != "locked") {
|
|
|
|
$("#drag").css('display', 'flex');
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
$("body").on('dragover', function(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
//ドロップした
|
|
|
|
$("body").on('drop', function(e) {
|
|
|
|
if (system != "locked") {
|
|
|
|
$("#drag").css('display', 'none');
|
|
|
|
e.preventDefault();
|
|
|
|
var files = e.originalEvent.dataTransfer.files;
|
|
|
|
pimg(files);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
//何もなくファイルが通過
|
|
|
|
$("#drag").on('dragleave', function(e) {
|
|
|
|
$("#drag").css('display', 'none');
|
|
|
|
});
|
|
|
|
|
|
|
|
//複数アップ
|
|
|
|
function pimg(files) {
|
|
|
|
console.log(files);
|
|
|
|
for (i = 0; i < files.length; i++) {
|
2018-03-13 04:41:38 +11:00
|
|
|
var dot=files[i].path.match(/\.(.+)$/)[1];
|
2018-02-24 03:02:44 +11:00
|
|
|
if(dot=="bmp" || dot=="BMP"){
|
2018-02-19 04:41:25 +11:00
|
|
|
var electron = require("electron");
|
|
|
|
var ipc = electron.ipcRenderer;
|
2018-04-10 02:22:08 +10:00
|
|
|
ipc.send('bmp-image', [files[i].path,i]);
|
2018-07-29 17:37:54 +10:00
|
|
|
todo(lang_progress[lang]);
|
2018-02-19 04:41:25 +11:00
|
|
|
|
|
|
|
}else{
|
2018-04-10 02:22:08 +10:00
|
|
|
handleFileUpload(files[i], obj,i);
|
2018-02-19 04:41:25 +11:00
|
|
|
}
|
2018-01-28 23:22:43 +11:00
|
|
|
}
|
|
|
|
}
|
2018-02-19 04:41:25 +11:00
|
|
|
var electron = require("electron");
|
|
|
|
var ipc = electron.ipcRenderer;
|
|
|
|
ipc.on('bmp-img-comp', function (event, b64) {
|
2018-04-10 02:22:08 +10:00
|
|
|
media(b64[0],"image/png",b64[1]);
|
2018-02-19 04:41:25 +11:00
|
|
|
});
|
2018-01-28 23:22:43 +11:00
|
|
|
//ドラッグ・アンド・ドロップを終了
|
|
|
|
function closedrop() {
|
|
|
|
$("#drag").css('display', 'none');
|
|
|
|
}
|
2018-03-18 02:00:53 +11:00
|
|
|
//ファイル選択
|
|
|
|
function fileselect() {
|
|
|
|
ipc.send('file-select', "");
|
|
|
|
}
|
2018-01-28 23:22:43 +11:00
|
|
|
|
2018-02-19 04:41:25 +11:00
|
|
|
//ファイル読み込み
|
2018-04-10 02:22:08 +10:00
|
|
|
function handleFileUpload(files, obj, no) {
|
2018-01-28 23:22:43 +11:00
|
|
|
var fr = new FileReader();
|
|
|
|
fr.onload = function(evt) {
|
|
|
|
var b64 = evt.target.result;
|
|
|
|
$('#b64-box').val(b64);
|
2018-04-10 02:22:08 +10:00
|
|
|
var ret = media(b64, files["type"], no)
|
2018-01-28 23:22:43 +11:00
|
|
|
}
|
|
|
|
fr.readAsDataURL(files);
|
|
|
|
$("#mec").append(files["name"] + "/");
|
|
|
|
}
|
|
|
|
|
|
|
|
//ファイルアップロード
|
2018-04-10 02:22:08 +10:00
|
|
|
function media(b64, type, no) {
|
|
|
|
var l = 4;
|
|
|
|
var c = "abcdefghijklmnopqrstuvwxyz0123456789";
|
|
|
|
var cl = c.length;
|
|
|
|
var r = "";
|
|
|
|
for(var i=0; i<l; i++){
|
|
|
|
r += c[Math.floor(Math.random()*cl)];
|
|
|
|
}
|
|
|
|
if ($("#media").val()) {
|
|
|
|
$("#media").val($("#media").val() + ',' + "tmp_"+r);
|
|
|
|
} else {
|
|
|
|
$("#media").val("tmp_"+r);
|
|
|
|
}
|
2018-09-19 02:41:48 +10:00
|
|
|
$(".toot-btn-group").prop("disabled", true);
|
2018-07-05 11:26:07 +10:00
|
|
|
$("#post-acct-sel").prop("disabled", true);
|
2018-02-19 04:41:25 +11:00
|
|
|
localStorage.setItem("image","busy");
|
2018-01-28 23:22:43 +11:00
|
|
|
todo("Image Upload...");
|
|
|
|
var media = toBlob(b64, type);
|
|
|
|
console.log(media);
|
|
|
|
var fd = new FormData();
|
|
|
|
fd.append('file', media);
|
|
|
|
var acct_id = $("#post-acct-sel").val();
|
|
|
|
var domain = localStorage.getItem("domain_" + acct_id);
|
2018-07-07 03:51:48 +10:00
|
|
|
var at = localStorage.getItem("acct_"+ acct_id + "_at");
|
2018-04-17 03:10:35 +10:00
|
|
|
var httpreq = new XMLHttpRequest();
|
2018-08-23 03:29:39 +10:00
|
|
|
if(localStorage.getItem("mode_" + domain)=="misskey"){
|
|
|
|
var start = "https://" + domain + "/api/drive/files/create";
|
2018-07-30 21:03:49 +10:00
|
|
|
httpreq.open('POST', start, true);
|
|
|
|
httpreq.upload.addEventListener("progress", progshow, false);
|
|
|
|
httpreq.responseType = 'json';
|
|
|
|
if ($("#nsfw").hasClass("nsfw-avail")) {
|
|
|
|
var nsfw = true;
|
|
|
|
} else {
|
|
|
|
var nsfw = false;
|
|
|
|
}
|
|
|
|
var previewer="url"
|
|
|
|
fd.append('i', at);
|
|
|
|
//fd.append('isSensitive', nsfw);
|
|
|
|
httpreq.send(fd);
|
|
|
|
}else{
|
|
|
|
var previewer="preview_url"
|
|
|
|
var start = "https://" + domain + "/api/v1/media";
|
|
|
|
httpreq.open('POST', start, true);
|
|
|
|
httpreq.upload.addEventListener("progress", progshow, false);
|
|
|
|
httpreq.responseType = 'json';
|
|
|
|
httpreq.setRequestHeader('Authorization', 'Bearer ' + at);
|
|
|
|
httpreq.send(fd);
|
|
|
|
}
|
2018-04-17 03:10:35 +10:00
|
|
|
httpreq.onreadystatechange = function() {
|
|
|
|
if (httpreq.readyState == 4) {
|
|
|
|
var json = httpreq.response;
|
|
|
|
console.log(json);
|
|
|
|
var img = localStorage.getItem("img");
|
2018-07-30 21:03:49 +10:00
|
|
|
if (json.type.indexOf("image")!=-1) {
|
|
|
|
var html = '<img src="' + json[previewer] + '" style="width:50px; max-height:100px;">';
|
2018-04-17 03:10:35 +10:00
|
|
|
$('#preview').append(html);
|
|
|
|
} else {
|
2018-07-29 17:37:54 +10:00
|
|
|
$('#preview').append(lang_postimg_previewdis[lang]);
|
2018-04-17 03:10:35 +10:00
|
|
|
}
|
|
|
|
if (!img) {
|
|
|
|
var img = "no-act";
|
|
|
|
}
|
|
|
|
if (img != "inline") {
|
|
|
|
var mediav=$("#media").val();
|
|
|
|
var regExp = new RegExp("tmp_"+r, "g");
|
|
|
|
mediav = mediav.replace(regExp, json["id"]);
|
|
|
|
$("#media").val(mediav);
|
|
|
|
|
|
|
|
}
|
|
|
|
if (img == "url") {
|
|
|
|
$("#textarea").val($("#textarea").val() + " " + json["text_url"])
|
|
|
|
}
|
|
|
|
todc();
|
2018-09-19 02:41:48 +10:00
|
|
|
$(".toot-btn-group").prop("disabled", false);
|
2018-04-17 03:10:35 +10:00
|
|
|
$('select').material_select();
|
2018-07-29 17:37:54 +10:00
|
|
|
$("#mec").text(lang_there[lang]);
|
|
|
|
Materialize.toast(lang_postimg_aftupload[lang], 1000);
|
2018-04-17 03:10:35 +10:00
|
|
|
$("#imgup").text("");
|
|
|
|
$("#imgsel").show();
|
|
|
|
localStorage.removeItem("image");
|
2018-02-19 04:41:25 +11:00
|
|
|
}
|
2018-04-17 03:10:35 +10:00
|
|
|
}
|
2018-01-28 23:22:43 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
//Base64からBlobへ
|
|
|
|
function toBlob(base64, type) {
|
|
|
|
var bin = atob(base64.replace(/^.*,/, ''));
|
|
|
|
var buffer = new Uint8Array(bin.length);
|
|
|
|
for (var i = 0; i < bin.length; i++) {
|
|
|
|
buffer[i] = bin.charCodeAt(i);
|
|
|
|
}
|
|
|
|
// Blobを作成
|
|
|
|
try {
|
|
|
|
var blob = new Blob([new Uint8Array(buffer)], {
|
|
|
|
type: type
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return blob;
|
2018-04-17 03:10:35 +10:00
|
|
|
}
|
|
|
|
//画像を貼り付けたら…
|
|
|
|
var element = document.querySelector("#textarea");
|
|
|
|
element.addEventListener("paste", function(e){
|
2018-09-06 02:47:27 +10:00
|
|
|
console.log(e)
|
2018-04-17 03:10:35 +10:00
|
|
|
// 画像の場合
|
|
|
|
// e.clipboardData.types.length == 0
|
|
|
|
// かつ
|
|
|
|
// e.clipboardData.types[0] == "Files"
|
|
|
|
// となっているので、それ以外を弾く
|
|
|
|
if (!e.clipboardData
|
|
|
|
|| !e.clipboardData.types
|
|
|
|
|| (e.clipboardData.types.length != 1)
|
|
|
|
|| (e.clipboardData.types[0] != "Files")) {
|
2018-08-23 03:29:39 +10:00
|
|
|
console.log("not image")
|
2018-04-17 03:10:35 +10:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ファイルとして得る
|
|
|
|
// (なぜかgetAsStringでは上手くいかなかった)
|
|
|
|
var imageFile = e.clipboardData.items[0].getAsFile();
|
|
|
|
|
|
|
|
// FileReaderで読み込む
|
|
|
|
var fr = new FileReader();
|
|
|
|
fr.onload = function(e) {
|
|
|
|
// onload内ではe.target.resultにbase64が入っているのであとは煮るなり焼くなり
|
|
|
|
var base64 = e.target.result;
|
|
|
|
var mediav = $("#media").val();
|
|
|
|
if(mediav){
|
2018-08-23 03:29:39 +10:00
|
|
|
var i=mediav.split(",").length;
|
2018-04-17 03:10:35 +10:00
|
|
|
}
|
|
|
|
media(base64, "image/png", i)
|
|
|
|
};
|
|
|
|
fr.readAsDataURL(imageFile);
|
|
|
|
|
|
|
|
// 画像以外がペーストされたときのために、元に戻しておく
|
2018-05-02 14:14:03 +10:00
|
|
|
});
|
|
|
|
//Adobeフォトエディタ
|
|
|
|
function adobe(){
|
|
|
|
var agree = localStorage.getItem("adobeagree");
|
|
|
|
ipc.send('adobe', agree);
|
|
|
|
}
|
|
|
|
ipc.on('adobeagree', function (event, arg) {
|
|
|
|
localStorage.setItem("adobeagree",arg);
|
|
|
|
});
|