| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
This is a wrapper for Ace Editor to provide a 2-panel diffing/merging tool that visualizes differences in two documents and allows users to copy changes from to the other.
It's built on top of @sanity/diff-match-patch library. That lib handles the hard part: the computation of the document diffs. Ace-diff just visualizes that information as line-diffs in the editors.
Take a look at demos on Ace-diff page. The demos illustrate a few different configurations and styles. Hopefully they'll give you a rough sense of what it does and how it works.
pnpm add ace-diff
pnpm add ace-builds
# or with npm
npm i ace-diff -S
npm i ace-builds -S
# or with yarn
yarn add ace-diff
yarn add ace-buildsimport AceDiff from 'ace-diff'
import * as ace from 'ace-builds'
// optionally, include CSS, or use your own
import 'ace-diff/styles.css'
// or import 'ace-diff/styles' (same file)Update your CSS import:
// Before (v3.x)
import 'ace-diff/dist/ace-diff.min.css'
// After (v4.x)
import 'ace-diff/styles.css'If you use ace-builds, ensure it's installed as a dependency:
pnpm add ace-buildsNote: ace-diff works with any Ace-compatible editor (Brace, CDN builds, etc.), so ace-builds is not strictly required.
TypeScript users get types automatically - no need for @types/ace-diff
<div class="acediff"></div>Here's an example of how you'd instantiate AceDiff.
const differ = new AceDiff({
ace: window.ace, // You Ace Editor instance
element: '.acediff',
left: {
content: 'your first file content here',
},
right: {
content: 'your second file content here',
},
})When using with Brace or any build system like Webpack, you can pass the editor as an option:
// If you are using Brace editor, you can pass it as well
const ace = require('brace')
const differ = new AceDiff({
ace, // using Brace
element: '.acediff',
left: {
content: 'your first file content here',
},
right: {
content: 'your second file content here',
},
})Because of the way how ACE is positioned, it's important to have Ace-diff running in some container with specified dimensions (and optionally with a position: relative)
Styling the elements is vitally important: the gutter should retain its width even if the user resizes his or her browser. But honestly, how you go about that is very much up to you: you can provide whatever CSS you want, depending on your scenario.
If you want the ace editor's to change height/width based on a user's browser, I find using flexbox the best option - but hell, if you want to use a <table>, knock yourself out. :)
Take a look at the demos for some ideas. They all use flexbox for the layouts, but include some different styles and class names just so you can see.
Ace-diff uses CSS custom properties (variables) for colors, making it easy to match your Ace editor theme. The default styles use a light theme.
Override these variables to customize the appearance:
.acediff {
--acediff-gutter-bg: #efefef; /* Gutter background */
--acediff-gutter-border: #bcbcbc; /* Gutter border color */
--acediff-diff-bg: #d8f2ff; /* Diff highlight background */
--acediff-diff-border: #a2d7f2; /* Diff highlight border */
--acediff-arrow-color: #000; /* Copy arrow color */
--acediff-arrow-shadow: rgba(255, 255, 255, 0.7); /* Arrow text shadow */
--acediff-arrow-hover-left: #004ea0; /* Left arrow hover color */
--acediff-arrow-hover-right: #c98100; /* Right arrow hover color */
}We provide a preset for the Twilight Ace theme. Import it after the base CSS:
import 'ace-diff/styles.css'
import 'ace-diff/styles-twilight.css'Then set the Ace editor theme to match:
const differ = new AceDiff({
theme: 'ace/theme/twilight',
// ... other options
})To create your own theme preset, simply override the CSS variables:
/* My custom dark theme */
.acediff {
--acediff-gutter-bg: #1e1e1e;
--acediff-gutter-border: #333;
--acediff-diff-bg: #264f78;
--acediff-diff-border: #1e3a5f;
--acediff-arrow-color: #fff;
--acediff-arrow-shadow: rgba(0, 0, 0, 0.7);
--acediff-arrow-hover-left: #569cd6;
--acediff-arrow-hover-right: #ce9178;
}You can configure your Ace-diff instance through a number of config settings. This object is what you pass to the constructor, like the JavaScript section above.
Here are all the defaults. I'll explain each one in details below. Note: you only need to override whatever you want.
{
ace: window.ace,
mode: null,
theme: null,
element: null,
diffGranularity: 'broad',
lockScrolling: true,
showDiffs: true,
showConnectors: true,
charDiffs: true,
maxDiffs: 5000,
left: {
id: null,
content: null,
mode: null,
theme: null,
editable: true,
copyLinkEnabled: true
},
right: {
id: null,
content: null,
mode: null,
theme: null,
editable: true,
copyLinkEnabled: true
},
classes: {
gutterID: 'acediff__gutter',
diff: 'acediff__diffLine',
diffChar: 'acediff__diffChar',
diffGutter: 'acediff__diffGutter',
connector: 'acediff__connector',
newCodeConnectorLink: 'acediff__newCodeConnector',
newCodeConnectorLinkContent: '→',
deletedCodeConnectorLink: 'acediff__deletedCodeConnector',
deletedCodeConnectorLinkContent: '←',
copyRightContainer: 'acediff__copy--right',
copyLeftContainer: 'acediff__copy--left',
},
connectorYOffset: 0,
onDiffReady: null,
}There are a few API methods available on your AceDiff instance.
All modern browsers. Open a ticket if you find otherwise.
Ace-diff is in active use in the COSMOS project and is maintained by the OpenC3 team. See the PluginDialog for a full example.
MIT.
| Back | FazBrowse Home | New Git URL |