All files / apps/wp-editor/app/components DiffSource.vue

0% Statements 0/22
0% Branches 0/9
0% Functions 0/5
0% Lines 0/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76                                                                                                                                                       
<template>
    <div class="flex h-full min-h-0 flex-col">
        <div class="grid grid-cols-2 divide-x divide-default border-b border-default">
            <p class="px-3 py-1.5 text-xs font-medium text-muted">Original</p>
            <p class="px-3 py-1.5 text-xs font-medium text-muted">
                Proposed <span v-if="Ieditable" class="text-dimmed">ยท editable</span>
            </p>
        </div>
        <div ref="host" class="min-h-0 flex-1 overflow-auto" />
    </div>
</template>
 
<script setup lang="ts">
import { html } from "@codemirror/lang-html";
import { MergeView } from "@codemirror/merge";
import { basicSetup, EditorView } from "codemirror";
 
interface Props {
    before: string;
    after: string;
    editable: boolean;
}
 
const props = defineProps<Props>();
const draft = defineModel<string>("draft", { required: true });
 
const host = useTemplateRef<HTMLElement>("host");
let view: MergeView | undefined;
 
const theme = EditorView.theme({
    "&": { backgroundColor: "var(--ui-bg)", color: "var(--ui-text)", fontSize: "13px" },
    ".cm-gutters": {
        backgroundColor: "var(--ui-bg-muted)",
        color: "var(--ui-text-dimmed)",
        border: "none",
    },
    ".cm-activeLine, .cm-activeLineGutter": { backgroundColor: "transparent" },
    ".cm-content": { fontFamily: "ui-monospace, SFMono-Regular, monospace" },
});
 
const build = () => {
    view?.destroy();
 
    if (!host.value) return;
 
    const shared = [basicSetup, html(), theme, EditorView.lineWrapping];
 
    view = new MergeView({
        parent: host.value,
        a: { doc: props.before, extensions: [...shared, EditorView.editable.of(false)] },
        b: {
            doc: draft.value,
            extensions: [
                ...shared,
                EditorView.editable.of(props.editable),
                EditorView.updateListener.of((update) => {
                    if (update.docChanged) draft.value = update.state.doc.toString();
                }),
            ],
        },
        collapseUnchanged: { margin: 3, minSize: 6 },
        gutter: true,
    });
};
 
onMounted(build);
onBeforeUnmount(() => view?.destroy());
 
// Rebuilt per article rather than patched, so undo history never crosses articles.
watch(() => [props.before, props.after, props.editable], build);
 
watch(draft, (value) => {
    if (view && value !== view.b.state.doc.toString()) build();
});
</script>