script.jsimport {
table, caption, thead, th, tbody, tr, td,
a, style,
} from '../easyDOMgenerator/script.js';
import {lightFormat} from '../date-fns.min.js/script.js';
export async function execute() {
const project = scrapbox.Project.name;
const title = scrapbox.Page.title;
const id = scrapbox.Page.id;
const res = await fetch(`/api/commits/${project}/${id}`)
const {commits} = await res.json();
const dom = table(
caption(
a(
{href: `../${project}/${title}`, target: '_blank', rel: 'noopener noreferrer'},
`/${project}/${title}`
),
'の編集履歴'
),
thead(th('Datetime'), th('Type'), th('Before'), th('After')),
tbody(...commits.flatMap(({created, changes}) => [
tr(
td(lightFormat(new Date(created * 1000), 'yyyy-MM-dd HH:mm:ss')),
td(getType(changes[0])),
td(changes[0].lines.origText ?? ''),
td(changes[0].lines.text ?? ''),
),
...changes.slice(1).flatMap(change => !getType(change) ? [] :
[tr(
td(''),
td(getType(change)),
td(change.lines.origText ?? ''),
td(change.lines.text ?? ''),
)]
),
])),
);
const tab = window.open();
tab.document.write(dom.outerHTML);
tab.document.close();
tab.document.head.appendChild(style(
`th {
text-align: left;
}
td {
white-space: nowrap;
}
`
));
}
function getType(change) {
return Object.keys(change).find(key => key.startsWith('_'))?.slice?.(1);
}