2018-01-28 23:22:43 +11:00
|
|
|
//ドラッグ・アンド・ドロップからアップロードまで。uiのimg.jsとは異なります。
|
|
|
|
var obj = $("body");
|
|
|
|
var system;
|
|
|
|
//ドラッグスタート
|
2019-05-19 17:39:30 +10:00
|
|
|
obj.on('dragstart', function (e) {
|
2018-01-28 23:22:43 +11:00
|
|
|
system = "locked"
|
|
|
|
});
|
|
|
|
//何もなくファイルが通過
|
2019-05-19 17:39:30 +10:00
|
|
|
obj.on('dragend', function (e) {
|
2018-01-28 23:22:43 +11:00
|
|
|
system = "";
|
|
|
|
});
|
|
|
|
//ドラッグファイルが画面上に
|
2019-05-19 17:39:30 +10:00
|
|
|
obj.on('dragenter', function (e) {
|
2018-01-28 23:22:43 +11:00
|
|
|
if (system != "locked") {
|
|
|
|
$("#drag").css('display', 'flex');
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2019-05-19 17:39:30 +10:00
|
|
|
$("body").on('dragover', function (e) {
|
2018-01-28 23:22:43 +11:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
//ドロップした
|
2019-05-19 17:39:30 +10:00
|
|
|
$("body").on('drop', function (e) {
|
2018-01-28 23:22:43 +11:00
|
|
|
if (system != "locked") {
|
|
|
|
$("#drag").css('display', 'none');
|
|
|
|
e.preventDefault();
|
|
|
|
var files = e.originalEvent.dataTransfer.files;
|
|
|
|
pimg(files);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
//何もなくファイルが通過
|
2019-05-19 17:39:30 +10:00
|
|
|
$("#drag").on('dragleave', function (e) {
|
2018-01-28 23:22:43 +11:00
|
|
|
$("#drag").css('display', 'none');
|
|
|
|
});
|
|
|
|
|
|
|
|
//複数アップ
|
|
|
|
function pimg(files) {
|
2019-05-19 16:17:05 +10:00
|
|
|
console.table(files);
|
2018-01-28 23:22:43 +11:00
|
|
|
for (i = 0; i < files.length; i++) {
|
2019-05-19 17:39:30 +10:00
|
|
|
var dot = files[i].path.match(/\.(.+)$/)[1];
|
|
|
|
if (dot == "bmp" || dot == "BMP") {
|
2019-06-15 02:10:32 +10:00
|
|
|
postMessage(["bmpImage", [files[i].path, i]], "*")
|
2019-05-19 17:39:30 +10:00
|
|
|
todo(lang.lang_progress);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
handleFileUpload(files[i], obj, i);
|
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() {
|
2019-06-15 03:17:28 +10:00
|
|
|
postMessage(["sendSinmpleIpc", "file-select"], "*")
|
2018-03-18 02:00:53 +11:00
|
|
|
}
|
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();
|
2019-05-19 17:39:30 +10:00
|
|
|
fr.onload = function (evt) {
|
2018-01-28 23:22:43 +11:00
|
|
|
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 = "";
|
2019-05-19 17:39:30 +10:00
|
|
|
for (var i = 0; i < l; i++) {
|
|
|
|
r += c[Math.floor(Math.random() * cl)];
|
2018-04-10 02:22:08 +10:00
|
|
|
}
|
|
|
|
if ($("#media").val()) {
|
2019-05-19 17:39:30 +10:00
|
|
|
$("#media").val($("#media").val() + ',' + "tmp_" + r);
|
2018-04-10 02:22:08 +10:00
|
|
|
} else {
|
2019-05-19 17:39:30 +10:00
|
|
|
$("#media").val("tmp_" + r);
|
2018-04-10 02:22:08 +10:00
|
|
|
}
|
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);
|
2019-05-19 17:39:30 +10:00
|
|
|
localStorage.setItem("image", "busy");
|
2018-01-28 23:22:43 +11:00
|
|
|
todo("Image Upload...");
|
|
|
|
var media = toBlob(b64, type);
|
|
|
|
var fd = new FormData();
|
|
|
|
fd.append('file', media);
|
|
|
|
var acct_id = $("#post-acct-sel").val();
|
|
|
|
var domain = localStorage.getItem("domain_" + acct_id);
|
2019-05-19 17:39:30 +10:00
|
|
|
var at = localStorage.getItem("acct_" + acct_id + "_at");
|
2018-04-17 03:10:35 +10:00
|
|
|
var httpreq = new XMLHttpRequest();
|
2019-05-19 17:39:30 +10:00
|
|
|
if (localStorage.getItem("mode_" + domain) == "misskey") {
|
2018-08-23 03:29:39 +10:00
|
|
|
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);
|
2019-03-08 05:19:26 +11:00
|
|
|
httpreq.responseType = "json";
|
2018-07-30 21:03:49 +10:00
|
|
|
if ($("#nsfw").hasClass("nsfw-avail")) {
|
|
|
|
var nsfw = true;
|
|
|
|
} else {
|
|
|
|
var nsfw = false;
|
|
|
|
}
|
2019-05-19 17:39:30 +10:00
|
|
|
var previewer = "url"
|
2018-07-30 21:03:49 +10:00
|
|
|
fd.append('i', at);
|
|
|
|
//fd.append('isSensitive', nsfw);
|
|
|
|
httpreq.send(fd);
|
2019-05-19 17:39:30 +10:00
|
|
|
} else {
|
|
|
|
var previewer = "preview_url"
|
2018-07-30 21:03:49 +10:00
|
|
|
var start = "https://" + domain + "/api/v1/media";
|
|
|
|
httpreq.open('POST', start, true);
|
|
|
|
httpreq.upload.addEventListener("progress", progshow, false);
|
2019-03-08 05:19:26 +11:00
|
|
|
httpreq.responseType = "json";
|
2018-07-30 21:03:49 +10:00
|
|
|
httpreq.setRequestHeader('Authorization', 'Bearer ' + at);
|
|
|
|
httpreq.send(fd);
|
|
|
|
}
|
2019-05-19 17:39:30 +10:00
|
|
|
httpreq.onreadystatechange = function () {
|
2019-03-08 05:19:26 +11:00
|
|
|
if (httpreq.readyState === 4) {
|
2018-04-17 03:10:35 +10:00
|
|
|
var json = httpreq.response;
|
|
|
|
var img = localStorage.getItem("img");
|
2019-05-19 17:39:30 +10:00
|
|
|
if (json.type.indexOf("image") != -1) {
|
|
|
|
var html = '<img src="' + json[previewer] + '" class="preview-img pointer" data-media="' + json["id"] + '" onclick="deleteImage(\'' + json["id"] + '\')" title="' + lang.lang_postimg_delete + '">';
|
2018-04-17 03:10:35 +10:00
|
|
|
$('#preview').append(html);
|
|
|
|
} else {
|
2019-01-26 14:24:26 +11:00
|
|
|
$('#preview').append(lang.lang_postimg_previewdis);
|
2018-04-17 03:10:35 +10:00
|
|
|
}
|
|
|
|
if (!img) {
|
|
|
|
var img = "no-act";
|
|
|
|
}
|
|
|
|
if (img != "inline") {
|
2019-05-19 17:39:30 +10:00
|
|
|
var mediav = $("#media").val();
|
|
|
|
var regExp = new RegExp("tmp_" + r, "g");
|
2018-04-17 03:10:35 +10:00
|
|
|
mediav = mediav.replace(regExp, json["id"]);
|
|
|
|
$("#media").val(mediav);
|
2019-05-19 17:39:30 +10:00
|
|
|
|
2018-04-17 03:10:35 +10:00
|
|
|
}
|
|
|
|
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);
|
2019-06-16 21:35:15 +10:00
|
|
|
$('select').formSelect();
|
2019-01-26 14:24:26 +11:00
|
|
|
$("#mec").text(lang.lang_there);
|
2019-06-07 02:11:04 +10:00
|
|
|
M.toast({ html: lang.lang_postimg_aftupload, displayLength: 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
|
|
|
}
|
|
|
|
//画像を貼り付けたら…
|
2019-05-19 17:39:30 +10:00
|
|
|
var element = document.querySelector("#textarea");
|
|
|
|
element.addEventListener("paste", function (e) {
|
|
|
|
if (!e.clipboardData || !e.clipboardData.items) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// DataTransferItemList に画像が含まれいない場合は終了する
|
2019-03-23 07:51:22 +11:00
|
|
|
var imageItems = [...e.clipboardData.items].filter(i => i.type.startsWith('image'));
|
2019-05-19 17:39:30 +10:00
|
|
|
if (imageItems.length == 0) {
|
2019-05-19 16:17:05 +10:00
|
|
|
console.warn("it is not image")
|
2019-05-19 17:39:30 +10:00
|
|
|
return true;
|
|
|
|
}
|
2018-04-17 03:10:35 +10:00
|
|
|
|
2019-05-19 17:39:30 +10:00
|
|
|
// ファイルとして得る
|
|
|
|
// DataTransferItem の kind は file なので getAsString ではなく getAsFile を呼ぶ
|
|
|
|
var imageFile = imageItems[0].getAsFile();
|
|
|
|
var imageType = imageItems[0].type;
|
2018-04-17 03:10:35 +10:00
|
|
|
|
2019-05-19 17:39:30 +10:00
|
|
|
// FileReaderで読み込む
|
|
|
|
var fr = new FileReader();
|
|
|
|
fr.onload = function (e) {
|
|
|
|
// onload内ではe.target.resultにbase64が入っているのであとは煮るなり焼くなり
|
2018-04-17 03:10:35 +10:00
|
|
|
var base64 = e.target.result;
|
|
|
|
var mediav = $("#media").val();
|
2019-05-19 17:39:30 +10:00
|
|
|
if (mediav) {
|
|
|
|
var i = mediav.split(",").length;
|
2018-04-17 03:10:35 +10:00
|
|
|
}
|
2019-05-19 17:39:30 +10:00
|
|
|
// DataTransferItem の type に mime tipes があるのでそれを使う
|
2019-03-23 07:51:22 +11:00
|
|
|
media(base64, imageType, i)
|
2019-05-19 17:39:30 +10:00
|
|
|
};
|
|
|
|
fr.readAsDataURL(imageFile);
|
2018-04-17 03:10:35 +10:00
|
|
|
|
2019-05-19 17:39:30 +10:00
|
|
|
// 画像以外がペーストされたときのために、元に戻しておく
|
2018-05-02 14:14:03 +10:00
|
|
|
});
|
2019-05-19 17:39:30 +10:00
|
|
|
function deleteImage(key) {
|
2019-06-22 02:06:32 +10:00
|
|
|
Swal.fire({
|
|
|
|
title: lang.lang_postimg_delete,
|
|
|
|
type: 'warning',
|
|
|
|
showCancelButton: true,
|
|
|
|
confirmButtonColor: '#3085d6',
|
|
|
|
cancelButtonColor: '#d33',
|
|
|
|
confirmButtonText: lang.lang_yesno,
|
|
|
|
cancelButtonText: lang.lang_no
|
|
|
|
}).then((result) => {
|
|
|
|
if (result.value) {
|
|
|
|
var media = $("#media").val();
|
|
|
|
var arr = media.split(",");
|
|
|
|
for (var i = 0; i < media.length; i++) {
|
|
|
|
if (arr[i] == key) {
|
|
|
|
arr.splice(i, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$("#media").val(arr.join(","));
|
|
|
|
$('#preview [data-media=' + key + ']').remove();
|
2019-03-21 07:49:59 +11:00
|
|
|
}
|
2019-06-22 02:06:32 +10:00
|
|
|
})
|
|
|
|
|
2019-03-21 07:49:59 +11:00
|
|
|
}
|