Skip to content

Commit

Permalink
convert to ES6 module
Browse files Browse the repository at this point in the history
  • Loading branch information
yumetodo committed Jan 10, 2020
1 parent fe7fbf4 commit d50e36a
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 85 deletions.
12 changes: 6 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<link href="https://fonts.googleapis.com/css?family=Baloo+Bhai&display=swap" rel="stylesheet">
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/style.css">
<script src="js/common.js"></script>
<script src="js/index.js"></script>
<script type="module" src="js/common.js"></script>
<script type="module" src="js/index.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/locale/ja.js"></script>
</head>
Expand All @@ -23,19 +23,19 @@ <h1><a href="./">mastogetter</a></h1>
<div class="area container flex-column">
<div>
<p><label for="load">インポートするまとめのリンク</label><input type="text" id="load" placeholder="https://hidao80.github.io/mastogetter/p.html?hoge"></p>
<input type="button" value="まとめを読み込む" onclick="loadPermalink()">
<input type="button" value="まとめを読み込む" id="loadPermalink">
</div>
<div id="domainid">
<p><label for="instance">インスタンス名</label><input type="text" id="instance" placeholder="https://qiitadon.com"></p>
<p><label for="toot-id">トゥートID or URL</label><input type="text" id="toot-id" placeholder="カーソルをあわせてヒントを表示"><span class="error">例)<br>012345678901234567<br>https://hogedon.com/web/status/012345678901234567<br>https://hogedon.com/@userid/012345678901234567</span></p>
<input type="button" value="ID or URLからプレビュー" onclick="showPreview()">
<input type="button" value="ID or URLからプレビュー" id="showPreview">
</div>
<div id="card-preview">
埋め込みカードのプレビューが表示されます
</div>
<div>
<a href="https://github.com/hidao80/mastogetter/blob/master/README.md">使い方</a>
<input type="button" value="トゥートを追加" onclick="addCard()">
<input type="button" value="トゥートを追加" id="addCard">
</div>
</div>
<div class="area">
Expand All @@ -44,7 +44,7 @@ <h1><a href="./">mastogetter</a></h1>
<label for="permalink">このまとめのリンク</label>
<span class="container">
<input id="permalink" type="text" placeholder="パーマリンクが生成されます" readonly="readonly">
<input id="copylink" type="button" value="コピー" onclick="copyPermalink()">
<input id="copylink" type="button" value="コピー">
</span>
</p>
</div>
Expand Down
70 changes: 40 additions & 30 deletions js/common.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
let card_list = [];
export let card_list = [];
let max_index = 0;

export function ready(loaded) {
if (["interactive", "complete"].includes(document.readyState)) {
loaded();
} else {
document.addEventListener("DOMContentLoaded", loaded);
}
}

function $(id) {
return document.getElementById(id);
}

const get_url_vars = (function () {
export const get_url_vars = (function() {
const vars = {};
const params = location.search.substring(1).split('&');
const params = location.search.substring(1).split("&");
for (let i = 0; i < params.length; i++) {
if (/=/.test(params[i])) {
const [key, val] = params[i].split('=');
const [key, val] = params[i].split("=");
vars[key] = val;
}
}
return vars;
})();

function deleteCard(index) { }
export function deleteCard(/*index*/) {}

function decodePermalink(get_url_vars) {
export function decodePermalink(get_url_vars) {
let instance_full = get_url_vars["i"];
if (instance_full.trim() == "") {
if (instance_full.trim() === "") {
instance_full = "https://qiitadon.com";
if ($("instance") !== null) {
$("instance").value = instance_full;
}
}
const instance = instance_full.split("//")[1];
const toot_id = get_url_vars["t"];
const toot_ids = toot_id.split(',');
const toot_ids = toot_id.split(",");
if (toot_ids[toot_ids.length - 1] < "1000000000000000") {
// 最後の要素が 1.0+E18より小さければ、
// id の途中で url が切れたと判断して最後の項目を
Expand All @@ -38,9 +46,9 @@ function decodePermalink(get_url_vars) {
}

return {
"instance_full": instance_full,
"instance": instance,
"toot_ids": toot_ids
instance_full: instance_full,
instance: instance,
toot_ids: toot_ids,
};
}

Expand All @@ -54,21 +62,21 @@ function genPermalink(toot_csv = undefined) {
}
}

function updatePermalinkFromCardList(){
console.log('Updaing permalink from card_list.');
function updatePermalinkFromCardList() {
console.log("Updaing permalink from card_list.");
let permalink = "https://hidao80.github.io/mastogetter/p.html?i=" + $("instance").value + "&t=";
Object.keys(card_list).forEach(function (key) {
Object.keys(card_list).forEach(function(key) {
permalink += card_list[key] + ",";
});
$("permalink").value = permalink;
}

function addPermalink(toot_csv) {
console.log('Adding CSV to permalink.');
console.log("Adding CSV to permalink.");
$("permalink").value += toot_csv;
}

function showCards(permalink_obj) {
export function showCards(permalink_obj) {
const instance_full = permalink_obj["instance_full"];
const instance = permalink_obj["instance"];
const toot_ids = permalink_obj["toot_ids"];
Expand All @@ -79,11 +87,11 @@ function showCards(permalink_obj) {
for (let i = 0; i < toot_ids.length; i++) {
toot_url = instance_full + "/api/v1/statuses/" + toot_ids[i];
xhr.open("GET", toot_url, false);
xhr.onload = function (e) {
xhr.onload = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
const toot = JSON.parse(xhr.responseText);
const timestamp = moment(toot.created_at).format('llll');
const timestamp = moment(toot.created_at).format("llll");
const toot_div = document.createElement("div");
toot_div.setAttribute("class", "toot");
let media = "";
Expand All @@ -97,8 +105,10 @@ function showCards(permalink_obj) {
<div class="e-content" lang="ja" style="display: block; direction: ltr"><p>${toot.content}</p></div>
${media}</div>
`;
toot_div.setAttribute("id", max_index);
toot_div.setAttribute("ondblclick", "deleteCard('" + max_index + "')");
toot_div.setAttribute("id", `o_${max_index}`);
toot_div.addEventListener("dblclick", () => {
deleteCard(card_list.length);
});
toot_div.setAttribute("draggable", "true");
toot_div.addEventListener("dragstart", handleDragStart, false);
toot_div.addEventListener("dragover", handleDragOver, false);
Expand All @@ -111,7 +121,7 @@ ${media}</div>
}
}
};
xhr.onerror = function (e) {
xhr.onerror = function() {
console.error(xhr.statusText);
};
xhr.send(null);
Expand All @@ -122,8 +132,8 @@ ${media}</div>
}

function handleDragStart(e) {
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text/plain', this.id);
e.dataTransfer.effectAllowed = "move";
e.dataTransfer.setData("text/plain", this.id);
}

function handleDragOver(e) {
Expand All @@ -136,13 +146,13 @@ function handleDrop(e) {
if (e.preventDefault) {
e.preventDefault();
}
e.dataTransfer.dropEffect = 'move';
e.dataTransfer.dropEffect = "move";
let node = e.target;
while (!node.getAttribute("ondblclick")) {
node = node.parentNode;
}
const src = $(e.dataTransfer.getData('text/plain'));
if (src.id == node.id) {
const src = $(e.dataTransfer.getData("text/plain"));
if (src.id === node.id) {
return;
}
const cards = $("cards");
Expand All @@ -158,10 +168,10 @@ function handleDrop(e) {
}
}
if (src_index < 0) {
return
return;
}
if (node_index < 0) {
return
return;
}

cards.removeChild(src);
Expand All @@ -171,13 +181,13 @@ function handleDrop(e) {
card_list.splice(node_index, 0, card_list[src_index]);
card_list.splice(src_index, 1);
} else {
const toot_id = card_list[src_index]
const toot_id = card_list[src_index];
card_list.splice(src_index, 1);
card_list.splice(node_index, 0, toot_id);
}
genPermalink();
}

function handleDragEnd(e) {
function handleDragEnd() {
console.log("drag end");
}
103 changes: 64 additions & 39 deletions js/index.js
Original file line number Diff line number Diff line change
@@ -1,103 +1,128 @@
import * as impl from "./common.js";

function $(id) {
return document.getElementById(id);
}

function showPreview() {
let instance_full = $("instance").value;
if (instance_full.trim() == "") {
if (instance_full.trim() === "") {
instance_full = "https://qiitadon.com";
$("instance").value = instance_full;
}
const instance = instance_full.split("//")[1];
const toot_id = $("toot-id").value.split("/").reverse()[0];
const toot_id = $("toot-id")
.value.split("/")
.reverse()[0];
const toot_url = instance_full + "/api/v1/statuses/" + toot_id;
const target_div = $("card-preview");

const xhr = new XMLHttpRequest();
xhr.open("GET", toot_url, true);
xhr.onload = function (e) {
xhr.onload = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
const toot = JSON.parse(xhr.responseText);
const timestamp = moment(toot.created_at).format('llll');
const timestamp = moment(toot.created_at).format("llll");
let media = "";
for (let i = 0; i < toot.media_attachments.length; i++) {
media += `<a href='${toot.media_attachments[i].url}'><img class='thumbs' src='${toot.media_attachments[i].preview_url}'></a>`;
}
target_div.innerHTML = `
<div class="toot">
<div class="box"><a href="${toot.account.url}" target="_blank"><img width="48" height="48" alt="" class="u-photo" src="${toot.account.avatar}"></a></div>
<div class="box"><a class="display-name" href="${toot.account.url}" target="_blank">${toot.account.display_name}<span>@${toot.account.username}@${(new URL(toot.account.url)).hostname}</span></a>
<a class="toot-time" href="${toot.url}" target="_blank">${timestamp}</a>
<div class="e-content" lang="ja" style="display: block; direction: ltr"><p>${toot.content}</p></div>
${media}</div>
</div>
`;
<div class="box">
<a href="${toot.account.url}" target="_blank">
<img width="48" height="48" alt="avatar" class="u-photo" src="${toot.account.avatar}">
</a>
</div>
<div class="box">
<a class="display-name" href="${toot.account.url}" target="_blank">
${toot.account.display_name}
<span>@${toot.account.username}@${new URL(toot.account.url).hostname}</span>
</a>
<a class="toot-time" href="${toot.url}" target="_blank">${timestamp}</a>
<div class="e-content" lang="ja" style="display: block; direction: ltr">
<p>${toot.content}</p>
</div>
${media}
</div>
</div>`;
} else {
console.error(xhr.statusText);
}
}
};
xhr.onerror = function (e) {
xhr.onerror = function() {
console.error(xhr.statusText);
};
xhr.send(null);
}

function addCard() {
const clone = $("card-preview").firstElementChild.cloneNode(true);
clone.setAttribute("id", max_index);
clone.setAttribute("ondblclick", "deleteCard('"+ max_index +"')");
clone.setAttribute("draggable", "true");
clone.addEventListener("dragstart", handleDragStart, false);
clone.addEventListener("dragover", handleDragOver, false);
clone.addEventListener("drop", handleDrop, false);
clone.addEventListener("dragend", handleDragEnd, false);
card_list[max_index] = $("toot-id").value.split("/").reverse()[0];
max_index++;
clone.setAttribute("id", `c_${impl.card_list.length}`);
clone.addEventListener("dblclick", () => {
deleteCard(impl.card_list.length);
});
clone.setAttribute("draggable", "true");
clone.addEventListener("dragstart", impl.handleDragStart, false);
clone.addEventListener("dragover", impl.handleDragOver, false);
clone.addEventListener("drop", impl.handleDrop, false);
clone.addEventListener("dragend", impl.handleDragEnd, false);
impl.card_list.push(
$("toot-id")
.value.split("/")
.reverse()[0]
);

$("cards").appendChild(clone);
genPermalink();
impl.genPermalink();
}

function deleteCard(index) {
$("cards").removeChild($(index));
delete card_list[index];
genPermalink();
impl.card_list.splice(index, 1);
delete impl.card_list[index];
impl.genPermalink();
}

function copyPermalink() {
if (isEmptyPermalink()) {
alertUsageNoPermalink();
return false;
}
genPermalink();
impl.genPermalink();
$("permalink").select();
document.execCommand("copy");
}

function loadPermalink() {
const permalink = $("load").value;
const permalink_str = {"i": permalink.split("?i=")[1].split("&")[0],
"t": permalink.split("&t=")[1]};
const permalink_obj = decodePermalink(permalink_str);

showCards(permalink_obj);
genPermalink(permalink_obj.toot_ids.join(","));
}
const permalink_str = { i: permalink.split("?i=")[1].split("&")[0], t: permalink.split("&t=")[1] };
const permalink_obj = impl.decodePermalink(permalink_str);

function alertUsageGitIO() {
alert("パーマリンクがコピーされました。\nこのあと https://git.io/xxxx 形式の短縮 URL を作成するため別ウィンドウがポップアップします。開かれた先に表示された値が URL の xxxx の部分になります。");
impl.showCards(permalink_obj);
impl.genPermalink(permalink_obj.toot_ids.join(","));
}

function alertUsageNoPermalink() {
alert("まとめられたリンクがありません。");
}

function isEmptyPermalink() {
return (!$("permalink").value) ? true : false;
return !$("permalink").value;
}

function submitGitIO() {
$("form-gitio").submit();
}
impl.ready(() => {
$("loadPermalink").addEventListener("click", () => {
loadPermalink();
});
$("showPreview").addEventListener("click", () => {
showPreview();
});
$("addCard").addEventListener("click", () => {
addCard();
});
$("copylink").addEventListener("click", () => {
copyPermalink();
});
});
Loading

0 comments on commit d50e36a

Please sign in to comment.