diff --git a/changelog.md b/changelog.md index bfc2654..7872544 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,8 @@ # Changelog +## 0.5.22 + - Add `MergeView` and `DiffView` to interop with the merge addon + ## 0.5.21 - update to CodeMirror 5.58.0 diff --git a/lib/codemirror.dart b/lib/codemirror.dart index dde32e4..d23aaae 100644 --- a/lib/codemirror.dart +++ b/lib/codemirror.dart @@ -1230,6 +1230,51 @@ class Token { String toString() => string; } +/// A wrapper around the MergeView class. +class MergeView extends ProxyHolder { + /// Create a new MergeView in the given element. See + /// https://codemirror.net/doc/manual.html#addon_merge for valid options values. + MergeView(Element element, Map options) : super(_create(element, options)); + + MergeView.fromProxy(JsObject proxy) : super(proxy); + + static JsObject _create(Element element, Map options) { + return JsObject( + context['CodeMirror']['MergeView'], [element, jsify(options)]); + } + + /// The reference to the edit property. + CodeMirror get edit => CodeMirror.fromJsObject(jsProxy['edit']); + + /// The reference to the left property. + DiffView get left => DiffView.fromJsObject(jsProxy['left']); +} + +/// A wrapper around the DiffView class. +class DiffView extends ProxyHolder { + static final Map _instances = {}; + + DiffView.fromProxy(JsObject proxy) : super(proxy); + + factory DiffView.fromJsObject(JsObject object) { + if (_instances.containsKey(object)) { + return _instances[object]; + } else { + return DiffView._fromJsObject(object); + } + } + + DiffView._fromJsObject(JsObject object) : super(object) { + _instances[jsProxy] = this; + } + + /// The reference to the edit property. + CodeMirror get edit => CodeMirror.fromJsObject(jsProxy['edit']); + + /// The reference to the orig property. + CodeMirror get orig => CodeMirror.fromJsObject(jsProxy['orig']); +} + /// A parent class for objects that can hold references to JavaScript objects. /// It has convenience methods for invoking methods on the JavaScript proxy, /// a method to add event listeners to the proxy, and a [dispose] method. diff --git a/pubspec.yaml b/pubspec.yaml index 46a4b1b..0680926 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -3,7 +3,7 @@ # license that can be found in the LICENSE file. name: codemirror -version: 0.5.21+5.58.0 +version: 0.5.22+5.58.0 description: A Dart wrapper around the CodeMirror text editor. homepage: https://github.com/google/codemirror.dart diff --git a/test/all_test.dart b/test/all_test.dart index 1544db7..0a89f9c 100644 --- a/test/all_test.dart +++ b/test/all_test.dart @@ -25,6 +25,7 @@ void main() { group('Doc', createDocTests); group('HtmlDoc', createHtmlDocTests); group('history', createHistoryTests); + group('MergeView', createMergeViewTests); } void createSimpleTests() { @@ -243,6 +244,46 @@ void createHistoryTests() { }); } +void createMergeViewTests() { + MergeView mergeView; + + const someMergeViewOptions = { + 'value': 'AAAAAA', + 'origLeft': 'BBBBBB', + 'showDifferences': true, + 'revertButtons': false, + }; + + mergeView = MergeView(editorHost, someMergeViewOptions); + + test('creates a MergeView class', () { + expect(mergeView, isA()); + }); + + test('has correct properties', () { + expect(mergeView.edit, isA()); + expect(mergeView.left, isA()); + }); + + group(DiffView, () { + DiffView diffView; + + setUp(() { + diffView = mergeView.left; + }); + + test('has correct properties', () { + expect(diffView.edit, isA()); + expect(diffView.orig, isA()); + }); + + test('has correct values', () { + expect(diffView.edit.getDoc().getValue(), equals('AAAAAA')); + expect(diffView.orig.getDoc().getValue(), equals('BBBBBB')); + }); + }); +} + void _expectHistory(Doc doc, int undo, int redo) { Map m = doc.historySize(); expect(m['undo'], undo); diff --git a/test/all_test.html b/test/all_test.html index c191ba7..0659d1b 100644 --- a/test/all_test.html +++ b/test/all_test.html @@ -11,6 +11,8 @@ Dart CodeMirror Tests + + diff --git a/tool/grind.dart b/tool/grind.dart index 74df5fe..6822596 100644 --- a/tool/grind.dart +++ b/tool/grind.dart @@ -76,6 +76,9 @@ String _concatenateModes(Directory dir) { files.add(joinFile(dir, ['addon', 'search', 'search.js'])); files.add(joinFile(dir, ['addon', 'search', 'searchcursor.js'])); + // Add merge addons. + files.add(joinFile(dir, ['addon', 'merge', 'merge.js'])); + // Required by some modes. files.add(joinFile(dir, ['addon', 'mode', 'overlay.js'])); files.add(joinFile(dir, ['addon', 'mode', 'simple.js']));