2022-08-06 04:25:16 +08:00
|
|
|
<script>
|
2022-08-06 13:03:34 +08:00
|
|
|
import { createEventDispatcher } from "svelte";
|
|
|
|
import ApiClient from "@/utils/ApiClient";
|
2022-08-06 04:25:16 +08:00
|
|
|
import OverlayPanel from "@/components/base/OverlayPanel.svelte";
|
2022-08-06 13:03:34 +08:00
|
|
|
import { addSuccessToast } from "@/stores/toasts";
|
2022-08-06 04:25:16 +08:00
|
|
|
|
2022-08-06 13:03:34 +08:00
|
|
|
const dispatch = createEventDispatcher();
|
2022-08-06 04:25:16 +08:00
|
|
|
|
|
|
|
let panel;
|
2022-08-06 13:03:34 +08:00
|
|
|
let oldCollections = [];
|
|
|
|
let newCollections = [];
|
|
|
|
let isImporting = false;
|
2022-08-06 04:25:16 +08:00
|
|
|
|
|
|
|
export function show(a, b) {
|
2022-08-06 13:03:34 +08:00
|
|
|
oldCollections = a;
|
|
|
|
newCollections = b;
|
2022-08-06 04:25:16 +08:00
|
|
|
|
|
|
|
panel?.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hide() {
|
|
|
|
return panel?.hide();
|
|
|
|
}
|
|
|
|
|
2022-08-06 13:03:34 +08:00
|
|
|
function diffsToHtml(diffs, ops = [window.DIFF_INSERT, window.DIFF_DELETE, window.DIFF_EQUAL]) {
|
2022-08-06 04:25:16 +08:00
|
|
|
const html = [];
|
|
|
|
const pattern_amp = /&/g;
|
|
|
|
const pattern_lt = /</g;
|
|
|
|
const pattern_gt = />/g;
|
|
|
|
const pattern_para = /\n/g;
|
|
|
|
|
|
|
|
for (let i = 0; i < diffs.length; i++) {
|
|
|
|
const op = diffs[i][0]; // operation (insert, delete, equal)
|
|
|
|
|
|
|
|
if (!ops.includes(op)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const text = diffs[i][1]
|
|
|
|
.replace(pattern_amp, "&")
|
|
|
|
.replace(pattern_lt, "<")
|
|
|
|
.replace(pattern_gt, ">")
|
|
|
|
.replace(pattern_para, "<br>");
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case DIFF_INSERT:
|
|
|
|
html[i] = '<ins class="block">' + text + "</ins>";
|
|
|
|
break;
|
|
|
|
case DIFF_DELETE:
|
|
|
|
html[i] = '<del class="block">' + text + "</del>";
|
|
|
|
break;
|
|
|
|
case DIFF_EQUAL:
|
|
|
|
html[i] = text;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return html.join("");
|
|
|
|
}
|
|
|
|
|
2022-08-06 13:03:34 +08:00
|
|
|
function diff(ops = [window.DIFF_INSERT, window.DIFF_DELETE, window.DIFF_EQUAL]) {
|
2022-08-06 04:25:16 +08:00
|
|
|
const dmp = new diff_match_patch();
|
2022-08-06 13:03:34 +08:00
|
|
|
const lines = dmp.diff_linesToChars_(
|
|
|
|
JSON.stringify(oldCollections, null, 4),
|
|
|
|
JSON.stringify(newCollections, null, 4)
|
|
|
|
);
|
2022-08-06 04:25:16 +08:00
|
|
|
const diffs = dmp.diff_main(lines.chars1, lines.chars2, false);
|
|
|
|
|
|
|
|
dmp.diff_charsToLines_(diffs, lines.lineArray);
|
|
|
|
|
|
|
|
return diffsToHtml(diffs, ops);
|
|
|
|
}
|
2022-08-06 13:03:34 +08:00
|
|
|
|
|
|
|
async function submitImport() {
|
|
|
|
if (isImporting) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isImporting = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await ApiClient.collections.import(newCollections);
|
|
|
|
addSuccessToast("Successfully imported the provided schema.");
|
|
|
|
dispatch("submit");
|
|
|
|
} catch (err) {
|
|
|
|
ApiClient.errorResponseHandler(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
hide();
|
|
|
|
|
|
|
|
isImporting = false;
|
|
|
|
}
|
2022-08-06 04:25:16 +08:00
|
|
|
</script>
|
|
|
|
|
2022-08-06 13:03:34 +08:00
|
|
|
<OverlayPanel bind:this={panel} class="full-width-popup import-popup" popup on:show on:hide>
|
2022-08-06 04:25:16 +08:00
|
|
|
<svelte:fragment slot="header">
|
2022-08-06 13:03:34 +08:00
|
|
|
<h4 class="center txt-break">Side-by-side diff</h4>
|
2022-08-06 04:25:16 +08:00
|
|
|
</svelte:fragment>
|
|
|
|
|
|
|
|
<div class="grid m-b-base">
|
|
|
|
<div class="col-6">
|
2022-08-06 13:03:34 +08:00
|
|
|
<div class="section-title">Old schema</div>
|
|
|
|
<code class="code-block">{@html diff([window.DIFF_DELETE, window.DIFF_EQUAL])}</code>
|
2022-08-06 04:25:16 +08:00
|
|
|
</div>
|
|
|
|
<div class="col-6">
|
2022-08-06 13:03:34 +08:00
|
|
|
<div class="section-title">New schema</div>
|
|
|
|
<code class="code-block">{@html diff([window.DIFF_INSERT, window.DIFF_EQUAL])}</code>
|
2022-08-06 04:25:16 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<svelte:fragment slot="footer">
|
|
|
|
<button type="button" class="btn btn-secondary" on:click={hide}>Close</button>
|
2022-08-06 13:03:34 +08:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="btn btn-expanded m-l-auto"
|
|
|
|
class:btn-loading={isImporting}
|
|
|
|
on:click={() => submitImport()}
|
|
|
|
>
|
|
|
|
<span class="txt">Confirm and import</span>
|
|
|
|
</button>
|
2022-08-06 04:25:16 +08:00
|
|
|
</svelte:fragment>
|
|
|
|
</OverlayPanel>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
code {
|
|
|
|
color: var(--txtHintColor);
|
|
|
|
min-height: 100%;
|
|
|
|
}
|
|
|
|
</style>
|