Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try adding diff viewer #730

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 121 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
"npm": ">=10.2.3"
},
"dependencies": {
"@codemirror/basic-setup": "^0.20.0",
"@codemirror/lang-json": "^6.0.1",
"@codemirror/state": "^6.4.1",
"@codemirror/view": "^6.33.0",
"@uiw/react-codemirror": "^4.23.1",
"@wordpress/icons": "^10.7.0",
"diff": "^7.0.0",
"lib-font": "^2.4.3"
},
"devDependencies": {
Expand Down
133 changes: 133 additions & 0 deletions src/editor-sidebar/code-mirror-diff-viewer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/**
* External dependencies
*/
import CodeMirror from '@uiw/react-codemirror';
import { EditorView, ViewPlugin, Decoration } from '@codemirror/view';

Check failure on line 5 in src/editor-sidebar/code-mirror-diff-viewer.js

View workflow job for this annotation

GitHub Actions / Lint

'ViewPlugin' is defined but never used
import { EditorState, RangeSetBuilder } from '@codemirror/state';

Check failure on line 6 in src/editor-sidebar/code-mirror-diff-viewer.js

View workflow job for this annotation

GitHub Actions / Lint

'EditorState' is defined but never used

Check failure on line 6 in src/editor-sidebar/code-mirror-diff-viewer.js

View workflow job for this annotation

GitHub Actions / Lint

'RangeSetBuilder' is defined but never used
import { basicSetup } from '@codemirror/basic-setup';

Check failure on line 7 in src/editor-sidebar/code-mirror-diff-viewer.js

View workflow job for this annotation

GitHub Actions / Lint

'basicSetup' is defined but never used
import { json } from '@codemirror/lang-json';
import { diffLines } from 'diff';

/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';

Check failure on line 14 in src/editor-sidebar/code-mirror-diff-viewer.js

View workflow job for this annotation

GitHub Actions / Lint

'__' is defined but never used

Check failure on line 14 in src/editor-sidebar/code-mirror-diff-viewer.js

View workflow job for this annotation

GitHub Actions / Lint

'sprintf' is defined but never used
import { useState, useEffect } from '@wordpress/element';

export function CodeMirrorDiffViewer({ oldCode, newCode }) {

Check failure on line 17 in src/editor-sidebar/code-mirror-diff-viewer.js

View workflow job for this annotation

GitHub Actions / Lint

Replace `{·oldCode,·newCode·}` with `·{·oldCode,·newCode·}·`
const [diff, setDiff] = useState([]);

Check failure on line 18 in src/editor-sidebar/code-mirror-diff-viewer.js

View workflow job for this annotation

GitHub Actions / Lint

Replace `····const·[diff,·setDiff]·=·useState([]` with `↹const·[·diff,·setDiff·]·=·useState(·[]·`

useEffect(() => {

Check failure on line 20 in src/editor-sidebar/code-mirror-diff-viewer.js

View workflow job for this annotation

GitHub Actions / Lint

Replace `····useEffect(` with `↹useEffect(·`
// override for testing

Check failure on line 21 in src/editor-sidebar/code-mirror-diff-viewer.js

View workflow job for this annotation

GitHub Actions / Lint

Replace `········` with `↹↹`
const oldCode = {
"hello": "123",
"world": "456",
"foo": "bar"
};
const newCode = {
"hello": "123",
"world": "456",
"foo": "baz"
};

setDiff(diffLines(JSON.stringify(oldCode, null, 4), JSON.stringify(newCode, null, 4)));
}, [oldCode, newCode]);

const diffDecorations = EditorView.decorations.compute([], (state) => getDiffDecorations(diff));
// const diffDecorations = (diff) => {
// const builder = new RangeSetBuilder();
// diff.forEach((part, index) => {
// if (part.added || part.removed) {
// const className = part.added ? 'added' : 'removed';
// const decoration = Decoration.mark({
// class: className,
// });
// builder.add(index, index + part.value.length, decoration);
// }
// });
// return builder.finish();
// };

// const diffPlugin = ViewPlugin.fromClass(class {
// constructor(view) {
// this.decorations = diffDecorations(diff);
// }
// update(update) {
// if (update.docChanged || update.viewportChanged) {
// this.decorations = diffDecorations(diff);
// }
// }
// }, {
// decorations: v => v.decorations
// });

// const state = EditorState.create({
// doc: newCode,
// extensions: [basicSetup, json(), diffPlugin]
// });

// return <CodeMirror state={state} />;

return (
<div>
<CodeMirror
value={ diff.map(part => part.value).join('') }
extensions={ [ json(), diffDecorations ] }
readOnly
/>
<style>
{`
.cbt-code-mirror-line-added { background-color: #D1F8D9; }
.cbt-code-mirror-line-removed { background-color: #FFCECB; }
`}
</style>
</div>
);
}

export function getDiffDecorations(diff) {
let decorations = [];
let lineNumber = 0;

diff.forEach(part => {
const lines = part.value.split('\n');
lines.forEach((line, index) => {
if (line === '') return; // Skip empty lines

const from = lineNumber + index;
const to = from; // Single line

if (part.added) {
decorations.push(Decoration.line({ class: "cbt-code-mirror-line-added" }).range(from, to));
} else if (part.removed) {
decorations.push(Decoration.line({ class: "cbt-code-mirror-line-removed" }).range(from, to));
}
});

lineNumber += lines.length - 1;
});

return Decoration.set(decorations);
}

export function mergeDeep(target, ...sources) {
if (!sources.length) return target;
const source = sources.shift();

if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
mergeDeep(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}

return mergeDeep(target, ...sources);
}

function isObject(item) {
return (item && typeof item === 'object' && !Array.isArray(item));
}
14 changes: 2 additions & 12 deletions src/editor-sidebar/json-editor-modal.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/**
* External dependencies
*/
import CodeMirror from '@uiw/react-codemirror';
import { json } from '@codemirror/lang-json';

/**
* WordPress dependencies
*/
Expand All @@ -16,6 +10,7 @@ import { useSelect } from '@wordpress/data';
* Internal dependencies
*/
import { fetchThemeJson } from '../resolvers';
import { CodeMirrorDiffViewer } from './code-mirror-diff-viewer';

const ThemeJsonEditorModal = ( { onRequestClose } ) => {
const [ themeData, setThemeData ] = useState( '' );
Expand All @@ -42,12 +37,7 @@ const ThemeJsonEditorModal = ( { onRequestClose } ) => {
onRequestClose={ onRequestClose }
className="create-block-theme__theme-json-modal"
>
<CodeMirror
extensions={ [ json() ] }
value={ themeData }
onChange={ handleSave }
readOnly
/>
<CodeMirrorDiffViewer oldCode={JSON.stringify(themeData, null, 2)} newCode={JSON.stringify(themeData, null, 2)} />
</Modal>
);
};
Expand Down
Loading