コードブロック記法の横に何かを表示する
仕様
どんな感じで実装できるか試してみる
最初からTypeScriptで書いてしまおう
やめた。何故かcompileできない
code:tsx
/// <reference no-default-lib="true"/>
/// <reference lib="esnext"/>
/// <reference lib="dom"/>
/** @jsx createElement */
export const Button = () => (
<div>
</div>
);
2021-08-25
14:34:20 調整終了
14:12:53 できた
code:js
script
fff
scrapbox
code:js
secrere
code:test.ts
分割を試す
code:test.ts
console.log(React)
code:script.ts
/// <reference no-default-lib="true"/>
/// <reference lib="esnext"/>
/// <reference lib="dom"/>
import {
eventName,
Scrapbox,
declare const scrapbox: Scrapbox;
type File = {
filename?: string;
lang: string;
/** コードブロックの開始行のid */ startIds: string[];
lines: string[];
};
const callback = () => {
const files = getCodeFiles();
div.remove()
);
files.forEach((file) => {
file.startIds.forEach((id) => {
const line = document.getElementById(L${id});
line?.insertBefore(
createButton({
onClick: ({ target }) => {
(target as HTMLDivElement).style.color = "red";
alert(file.lines.join("\n"));
setTimeout(
() => (target as HTMLDivElement).style.removeProperty("color"),
1000,
);
},
}),
line?.firstElementChild,
);
});
});
};
callback();
scrapbox.addListener("lines:changed" as eventName, callback);
function getCodeFiles() {
const codeBlocks =
scrapbox.Page.lines?.flatMap((line) => "codeBlock" in line ? line : []) ?? [];
return codeBlocks.reduce((acc: File[], { codeBlock, text, id }) => {
const sameFileIndex = acc.findIndex(({ filename }) =>
filename !== undefined && filename === codeBlock.filename
);
// code blockの先頭かつ新しいコードブロックのときのみ新しいfileを追加する
if (codeBlock.start && sameFileIndex < 0) {
return [...acc, {
filename: codeBlock.filename,
lang: codeBlock.lang,
lines: [],
}];
}
if (codeBlock.start) {
acc.at(sameFileIndex)?.startIds?.push?.(id);
} else {
// 既存のコードブロックもしくは末尾のコードブロックに追記する
acc.at(sameFileIndex)?.lines?.push?.(text);
}
return acc;
}, []);
}
function createButton(props: { onClick: (event: MouseEvent) => void }) {
const div = document.createElement("div");
div.style.position = "absolute";
div.style.zIndex = "900";
div.textContent = "▶";
div.dataset.id = "jupyter-button";
div.addEventListener("click", props.onClick);
return div;
}
code:script.js
var l=()=>{let s=a();document.querySelectorAll('data-id="jupyter-button"').forEach(e=>e.remove()),s.forEach(e=>{e.startIds.forEach(t=>{let r=document.getElementById(L${t});r?.insertBefore(d({onClick:({target:n})=>{n.style.color="red",alert(e.lines.join(` `)),setTimeout(()=>n.style.removeProperty("color"),1e3)}}),r?.firstElementChild)})})};l();scrapbox.addListener("lines:changed",l);function a(){return(scrapbox.Page.lines?.flatMap(e=>"codeBlock"in e?e:[])??[]).reduce((e,{codeBlock:t,text:r,id:n})=>{let o=e.findIndex(({filename:i})=>i!==void 0&&i===t.filename);return t.start&&o<0?[...e,{filename:t.filename,lang:t.lang,startIds:n,lines:[]}]:(t.start?e.at(o)?.startIds?.push?.(n):e.at(o)?.lines?.push?.(r),e)},[])}function d(s){let e=document.createElement("div");return e.style.position="absolute",e.style.left="-1em",e.style.zIndex="900",e.textContent="▶",e.dataset.id="jupyter-button",e.addEventListener("click",s.onClick),e} //# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsic2NyaXB0LnRzIl0sCiAgInNvdXJjZXNDb250ZW50IjogWyIvLy8gPHJlZmVyZW5jZSBuby1kZWZhdWx0LWxpYj1cInRydWVcIi8+XG4vLy8gPHJlZmVyZW5jZSBsaWI9XCJlc25leHRcIi8+XG4vLy8gPHJlZmVyZW5jZSBsaWI9XCJkb21cIi8+XG5cbmltcG9ydCB7XG4gIGV2ZW50TmFtZSxcbiAgU2NyYXBib3gsXG59IGZyb20gXCJodHRwczovL3Jhdy5naXRodWJ1c2VyY29udGVudC5jb20vc2NyYXBib3gtanAvdHlwZXMvMC4wLjUvbW9kLnRzXCI7XG5kZWNsYXJlIGNvbnN0IHNjcmFwYm94OiBTY3JhcGJveDtcblxudHlwZSBGaWxlID0ge1xuICBmaWxlbmFtZT86IHN0cmluZztcbiAgbGFuZzogc3RyaW5nO1xuICAvKiog44Kz44O844OJ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