Skip to content

Commit

Permalink
#63
Browse files Browse the repository at this point in the history
  • Loading branch information
scniro committed Mar 4, 2018
1 parent 85ff3e2 commit 8a8755b
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 82 deletions.
2 changes: 1 addition & 1 deletion docs/app.min.js

Large diffs are not rendered by default.

84 changes: 44 additions & 40 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,46 +48,37 @@ var Shared = (function () {
this.editor.focus();
}
};
Shared.prototype.apply = function (props, next, preserved) {
if (next) {
if (next.selection) {
if (next.selection.ranges) {
if (props.selection) {
if (!Helper.equals(props.selection.ranges, next.selection.ranges)) {
this.delegateSelection(next.selection.ranges, next.selection.focus || false);
}
}
else {
this.delegateSelection(next.selection.ranges, next.selection.focus || false);
}
}
}
if (next.cursor) {
if (props.cursor) {
if (!Helper.equals(props.cursor, next.cursor)) {
this.delegateCursor(preserved.cursor || next.cursor, (next.autoScroll || false), (next.autoCursor || false));
}
}
else {
this.delegateCursor(preserved.cursor || next.cursor, (next.autoScroll || false), (next.autoCursor || false));
}
}
if (next.scroll) {
this.delegateScroll(next.scroll);
}
Shared.prototype.apply = function (props) {
if (props && props.selection && props.selection.ranges) {
this.delegateSelection(props.selection.ranges, props.selection.focus || false);
}
else {
if (props.selection) {
if (props.selection.ranges) {
this.delegateSelection(props.selection.ranges, props.selection.focus || false);
}
}
if (props.cursor) {
this.delegateCursor(props.cursor, (props.autoScroll || false), (props.autoFocus || false));
}
if (props.scroll) {
if (props && props.cursor) {
this.delegateCursor(props.cursor, (props.autoScroll || false), (props.autoFocus || false));
}
if (props && props.scroll) {
this.delegateScroll(props.scroll);
}
};
Shared.prototype.applyNext = function (props, next, preserved) {
if (props && props.selection && props.selection.ranges) {
next && next.selection && next.selection.ranges && !Helper.equals(props.selection.ranges, next.selection.ranges) ?
this.delegateSelection(next.selection.ranges, next.selection.focus || false) :
this.delegateSelection(props.selection.ranges, props.selection.focus || false);
}
if (props && props.cursor) {
next && next.cursor && !Helper.equals(props.cursor, next.cursor) ?
this.delegateCursor(preserved.cursor || next.cursor, (next.autoScroll || false), (next.autoCursor || false)) :
this.delegateCursor(preserved.cursor || props.cursor, (props.autoScroll || false), (props.autoFocus || false));
}
if (props && props.scroll) {
next && next.scroll && !Helper.equals(props.scroll, next.scroll) ?
this.delegateScroll(next.scroll) :
this.delegateScroll(props.scroll);
}
}
};
Shared.prototype.applyStatic = function (props, preserved) {
if (preserved && preserved.cursor) {
this.delegateCursor(preserved.cursor || props.cursor, (props.autoScroll || false), (props.autoFocus || false));
}
};
Shared.prototype.wire = function (name) {
Expand Down Expand Up @@ -378,7 +369,8 @@ var Controlled = (function (_super) {
preserved.cursor = this.editor.getCursor();
}
this.hydrate(nextProps);
this.shared.apply(this.props, nextProps, preserved);
this.shared.applyNext(this.props, nextProps, preserved);
this.shared.applyStatic(this.props, preserved);
};
Controlled.prototype.componentWillUnmount = function () {
if (SERVER_RENDERED)
Expand Down Expand Up @@ -406,6 +398,8 @@ var UnControlled = (function (_super) {
var _this = _super.call(this, props) || this;
if (SERVER_RENDERED)
return _this;
_this.applied = false;
_this.appliedStatic = false;
_this.continueChange = false;
_this.hydrated = false;
_this.initCb = function () {
Expand Down Expand Up @@ -477,6 +471,7 @@ var UnControlled = (function (_super) {
});
this.hydrate(this.props);
this.shared.apply(this.props);
this.applied = true;
this.mounted = true;
if (this.props.onBlur)
this.shared.wire('onBlur');
Expand Down Expand Up @@ -519,12 +514,21 @@ var UnControlled = (function (_super) {
var preserved = { cursor: null };
if (nextProps.value !== this.props.value) {
this.hydrated = false;
this.applied = false;
this.appliedStatic = false;
}
if (!this.props.autoCursor && this.props.autoCursor !== undefined) {
preserved.cursor = this.editor.getCursor();
}
this.hydrate(nextProps);
this.shared.apply(this.props, nextProps, preserved);
if (!this.applied) {
this.shared.apply(this.props);
this.applied = true;
}
if (!this.appliedStatic) {
this.shared.applyStatic(this.props, preserved);
this.appliedStatic = true;
}
};
UnControlled.prototype.componentWillUnmount = function () {
if (SERVER_RENDERED)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-codemirror2",
"version": "4.0.1",
"version": "4.0.1-test-pkg",
"description": "a tiny react codemirror component wrapper",
"main": "index.js",
"typings": "index.d.ts",
Expand Down
104 changes: 64 additions & 40 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ export interface IUnControlledCodeMirror extends ICodeMirror {

declare interface ICommon {
wire: (name: string) => void;
apply: (props: IControlledCodeMirror | IUnControlledCodeMirror, next?: IControlledCodeMirror | IUnControlledCodeMirror, preserved?: IPreservedOptions) => void;
apply: (props: IControlledCodeMirror | IUnControlledCodeMirror) => void;
applyNext: (props: IControlledCodeMirror | IUnControlledCodeMirror, next?: IControlledCodeMirror | IUnControlledCodeMirror, preserved?: IPreservedOptions) => void;
applyStatic: (props: IControlledCodeMirror | IUnControlledCodeMirror, preserved?: IPreservedOptions) => void;
}

declare interface IPreservedOptions {
Expand Down Expand Up @@ -139,49 +141,51 @@ class Shared implements ICommon {
}
}

public apply(props: IControlledCodeMirror | IUnControlledCodeMirror, next?: IControlledCodeMirror | IUnControlledCodeMirror, preserved?: any) {

if (next) {
if (next.selection) {
if (next.selection.ranges) {
if (props.selection) {
if (!Helper.equals(props.selection.ranges, next.selection.ranges)) {
this.delegateSelection(next.selection.ranges, next.selection.focus || false);
}
} else {
this.delegateSelection(next.selection.ranges, next.selection.focus || false);
}
}
}
public apply(props: IControlledCodeMirror | IUnControlledCodeMirror) {

if (next.cursor) {
if (props.cursor) {
if (!Helper.equals(props.cursor, next.cursor)) {
this.delegateCursor(preserved.cursor || next.cursor, (next.autoScroll || false), (next.autoCursor || false));
}
} else {
this.delegateCursor(preserved.cursor || next.cursor, (next.autoScroll || false), (next.autoCursor || false));
}
}
// init ranges
if (props && props.selection && props.selection.ranges) {
this.delegateSelection(props.selection.ranges, props.selection.focus || false);
}

if (next.scroll) {
this.delegateScroll(next.scroll);
}
// init cursor
if (props && props.cursor) {
this.delegateCursor(props.cursor, (props.autoScroll || false), (props.autoFocus || false));
}

} else {
if (props.selection) {
if (props.selection.ranges) {
this.delegateSelection(props.selection.ranges, props.selection.focus || false);
}
}
// init scroll
if (props && props.scroll) {
this.delegateScroll(props.scroll);
}
}

if (props.cursor) {
this.delegateCursor(props.cursor, (props.autoScroll || false), (props.autoFocus || false));
}
public applyNext(props: IControlledCodeMirror | IUnControlledCodeMirror, next?: IControlledCodeMirror | IUnControlledCodeMirror, preserved?: any) {

if (props.scroll) {
// handle new ranges
if (props && props.selection && props.selection.ranges) {
next && next.selection && next.selection.ranges && !Helper.equals(props.selection.ranges, next.selection.ranges) ?
this.delegateSelection(next.selection.ranges, next.selection.focus || false) :
this.delegateSelection(props.selection.ranges, props.selection.focus || false);
}

// handle new cursor
if (props && props.cursor) {
next && next.cursor && !Helper.equals(props.cursor, next.cursor) ?
this.delegateCursor(preserved.cursor || next.cursor, (next.autoScroll || false), (next.autoCursor || false)) :
this.delegateCursor(preserved.cursor || props.cursor, (props.autoScroll || false), (props.autoFocus || false));
}

// handle new scroll
if (props && props.scroll) {
next && next.scroll && !Helper.equals(props.scroll, next.scroll) ?
this.delegateScroll(next.scroll) :
this.delegateScroll(props.scroll);
}
}
}

public applyStatic(props: IControlledCodeMirror | IUnControlledCodeMirror, preserved?: any) {
if (preserved && preserved.cursor) {
this.delegateCursor(preserved.cursor || props.cursor, (props.autoScroll || false), (props.autoFocus || false));
}
}

Expand Down Expand Up @@ -511,7 +515,9 @@ export class Controlled extends React.Component<IControlledCodeMirror, any> {

this.hydrate(nextProps);

this.shared.apply(this.props, nextProps, preserved);
this.shared.applyNext(this.props, nextProps, preserved);

this.shared.applyStatic(this.props, preserved);
}

/** @internal */
Expand Down Expand Up @@ -560,13 +566,19 @@ export class UnControlled extends React.Component<IUnControlledCodeMirror, any>
private ref: HTMLElement;
/** @internal */
private shared: Shared;
/** @internal */
private applied: boolean;
/** @internal */
private appliedStatic: boolean;

/** @internal */
constructor(props: IUnControlledCodeMirror) {
super(props);

if (SERVER_RENDERED) return;

this.applied = false;
this.appliedStatic = false;
this.continueChange = false;
this.hydrated = false;
this.initCb = () => {
Expand Down Expand Up @@ -659,6 +671,8 @@ export class UnControlled extends React.Component<IUnControlledCodeMirror, any>

this.shared.apply(this.props);

this.applied = true;

this.mounted = true;

if (this.props.onBlur) this.shared.wire('onBlur');
Expand Down Expand Up @@ -693,6 +707,8 @@ export class UnControlled extends React.Component<IUnControlledCodeMirror, any>

if (nextProps.value !== this.props.value) {
this.hydrated = false;
this.applied = false;
this.appliedStatic = false;
}

if (!this.props.autoCursor && this.props.autoCursor !== undefined) {
Expand All @@ -701,7 +717,15 @@ export class UnControlled extends React.Component<IUnControlledCodeMirror, any>

this.hydrate(nextProps);

this.shared.apply(this.props, nextProps, preserved);
if (!this.applied) {
this.shared.apply(this.props);
this.applied = true;
}

if (!this.appliedStatic) {
this.shared.applyStatic(this.props, preserved);
this.appliedStatic = true;
}
}

/** @internal */
Expand Down

0 comments on commit 8a8755b

Please sign in to comment.