Skip to content

Commit

Permalink
πŸ§Ÿβ€β™€οΈπŸ§žβ€β™‚οΈ Merge pull request #732 from bustle/zeejab/prettier
Browse files Browse the repository at this point in the history
Prettier + ESLint πŸ§Ÿβ€β™€οΈπŸ§žβ€β™‚οΈ
  • Loading branch information
ZeeJab committed May 29, 2020
2 parents 1c4e21a + bfcd8cf commit 83cd11c
Show file tree
Hide file tree
Showing 89 changed files with 5,639 additions and 5,000 deletions.
629 changes: 347 additions & 282 deletions .eslintrc.js

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ editor.render(element);

Fork the repo, write a test, make a change, open a PR.

### Tests
### Tests, Linting, Formatting

Install dependencies via yarn:

Expand All @@ -467,6 +467,14 @@ Or run headless tests via testem:

Tests in CI are run at Travis via Saucelabs (see the `test:ci` yarn script).

Run linter

* `yarn lint`

Run formatting

* `yarn format`

### Demo

To run the demo site locally:
Expand Down
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"scripts": {
"start": "rollup -c --watch",
"test:ci": "yarn build:docs && yarn build && testem ci -f testem-ci.js",
"test": "yarn build && testem ci -f testem.js",
"test": "yarn lint && yarn build && testem ci -f testem.js",
"lint": "prettier src --check && eslint src",
"format": "prettier src --write",
"build": "rollup -c",
"build:docs": "jsdoc -c ./.jsdoc",
"build:website": "yarn build:docs && yarn build && ./bin/build-website.sh",
Expand Down Expand Up @@ -50,8 +52,11 @@
"@rollup/plugin-commonjs": "^11.1.0",
"@rollup/plugin-node-resolve": "^7.1.3",
"conventional-changelog-cli": "^2.0.34",
"eslint": "^7.1.0",
"eslint-config-prettier": "^6.11.0",
"jquery": "^3.5.1",
"jsdoc": "^3.6.4",
"prettier": "^2.0.5",
"qunit": "^2.10.0",
"rollup": "^2.10.3",
"rollup-plugin-copy": "^3.3.0",
Expand All @@ -60,6 +65,12 @@
"saucie": "^3.3.3",
"testem": "^3.1.0"
},
"prettier": {
"arrowParens": "avoid",
"printWidth": 120,
"semi": false,
"singleQuote": true
},
"volta": {
"node": "12.14.1",
"yarn": "1.22.1"
Expand Down
28 changes: 18 additions & 10 deletions src/css/mobiledoc-kit.css
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,34 @@
*/

@-webkit-keyframes fade-in {
0% { opacity: 0; }
100% { opacity: 1; }
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@keyframes fade-in {
0% { opacity: 0; }
100% { opacity: 1; }
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}

.__mobiledoc-tooltip {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 0.7em;
white-space: nowrap;
position: absolute;
background-color: rgba(43,43,43,0.9);
background-color: rgba(43, 43, 43, 0.9);
border-radius: 3px;
line-height: 1em;
padding: 0.7em 0.9em;
color: #FFF;
color: #fff;
-webkit-animation: fade-in 0.2s;
animation: fade-in 0.2s;
animation: fade-in 0.2s;
}

.__mobiledoc-tooltip:before {
Expand All @@ -126,7 +134,7 @@
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-bottom: 5px solid rgba(43,43,43,0.9);
border-bottom: 5px solid rgba(43, 43, 43, 0.9);
top: -5px;
margin-left: -5px;
}
Expand All @@ -142,7 +150,7 @@
}

.__mobiledoc-tooltip a {
color: #FFF;
color: #fff;
text-decoration: none;
}

Expand Down
14 changes: 7 additions & 7 deletions src/js/cards/image.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import placeholderImageSrc from 'mobiledoc-kit/utils/placeholder-image-src';
import placeholderImageSrc from 'mobiledoc-kit/utils/placeholder-image-src'

export default {
name: 'image',
type: 'dom',

render({payload}) {
let img = document.createElement('img');
img.src = payload.src || placeholderImageSrc;
return img;
}
};
render({ payload }) {
let img = document.createElement('img')
img.src = payload.src || placeholderImageSrc
return img
},
}
110 changes: 53 additions & 57 deletions src/js/editor/edit-history.js
Original file line number Diff line number Diff line change
@@ -1,126 +1,122 @@
import mobiledocParsers from 'mobiledoc-kit/parsers/mobiledoc';
import FixedQueue from 'mobiledoc-kit/utils/fixed-queue';
import mobiledocParsers from 'mobiledoc-kit/parsers/mobiledoc'
import FixedQueue from 'mobiledoc-kit/utils/fixed-queue'

function findLeafSectionAtIndex(post, index) {
let section;
let section
post.walkAllLeafSections((_section, _index) => {
if (index === _index) {
section = _section;
section = _section
}
});
return section;
})
return section
}

export class Snapshot {
constructor(takenAt, editor, editAction=null) {
this.mobiledoc = editor.serialize();
this.editor = editor;
this.editAction = editAction;
this.takenAt = takenAt;
constructor(takenAt, editor, editAction = null) {
this.mobiledoc = editor.serialize()
this.editor = editor
this.editAction = editAction
this.takenAt = takenAt

this.snapshotRange();
this.snapshotRange()
}

snapshotRange() {
let { range, cursor } = this.editor;
let { range, cursor } = this.editor
if (cursor.hasCursor() && !range.isBlank) {
let { head, tail } = range;
let { head, tail } = range
this.range = {
head: [head.leafSectionIndex, head.offset],
tail: [tail.leafSectionIndex, tail.offset]
};
tail: [tail.leafSectionIndex, tail.offset],
}
}
}

getRange(post) {
if (this.range) {
let { head, tail } = this.range;
let [headLeafSectionIndex, headOffset] = head;
let [tailLeafSectionIndex, tailOffset] = tail;
let headSection = findLeafSectionAtIndex(post, headLeafSectionIndex);
let tailSection = findLeafSectionAtIndex(post, tailLeafSectionIndex);
let { head, tail } = this.range
let [headLeafSectionIndex, headOffset] = head
let [tailLeafSectionIndex, tailOffset] = tail
let headSection = findLeafSectionAtIndex(post, headLeafSectionIndex)
let tailSection = findLeafSectionAtIndex(post, tailLeafSectionIndex)

head = headSection.toPosition(headOffset);
tail = tailSection.toPosition(tailOffset);
head = headSection.toPosition(headOffset)
tail = tailSection.toPosition(tailOffset)

return head.toRange(tail);
return head.toRange(tail)
}
}

groupsWith(groupingTimeout, editAction, takenAt) {
return (
editAction !== null &&
this.editAction === editAction &&
this.takenAt + groupingTimeout > takenAt
);
return editAction !== null && this.editAction === editAction && this.takenAt + groupingTimeout > takenAt
}
}

export default class EditHistory {
constructor(editor, queueLength, groupingTimeout) {
this.editor = editor;
this._undoStack = new FixedQueue(queueLength);
this._redoStack = new FixedQueue(queueLength);
this.editor = editor
this._undoStack = new FixedQueue(queueLength)
this._redoStack = new FixedQueue(queueLength)

this._pendingSnapshot = null;
this._groupingTimeout = groupingTimeout;
this._pendingSnapshot = null
this._groupingTimeout = groupingTimeout
}

snapshot() {
// update the current snapshot with the range read from DOM
if (this._pendingSnapshot) {
this._pendingSnapshot.snapshotRange();
this._pendingSnapshot.snapshotRange()
}
}

storeSnapshot(editAction=null) {
let now = Date.now();
storeSnapshot(editAction = null) {
let now = Date.now()
// store pending snapshot
let pendingSnapshot = this._pendingSnapshot;
let pendingSnapshot = this._pendingSnapshot
if (pendingSnapshot) {
if (!pendingSnapshot.groupsWith(this._groupingTimeout, editAction, now)) {
this._undoStack.push(pendingSnapshot);
this._undoStack.push(pendingSnapshot)
}
this._redoStack.clear();
this._redoStack.clear()
}

// take new pending snapshot to store next time `storeSnapshot` is called
this._pendingSnapshot = new Snapshot(now, this.editor, editAction);
this._pendingSnapshot = new Snapshot(now, this.editor, editAction)
}

stepBackward(postEditor) {
// Throw away the pending snapshot
this._pendingSnapshot = null;
this._pendingSnapshot = null

let snapshot = this._undoStack.pop();
let snapshot = this._undoStack.pop()
if (snapshot) {
this._redoStack.push(new Snapshot(Date.now(), this.editor));
this._restoreFromSnapshot(snapshot, postEditor);
this._redoStack.push(new Snapshot(Date.now(), this.editor))
this._restoreFromSnapshot(snapshot, postEditor)
}
}

stepForward(postEditor) {
let snapshot = this._redoStack.pop();
let snapshot = this._redoStack.pop()
if (snapshot) {
this._undoStack.push(new Snapshot(Date.now(), this.editor));
this._restoreFromSnapshot(snapshot, postEditor);
this._undoStack.push(new Snapshot(Date.now(), this.editor))
this._restoreFromSnapshot(snapshot, postEditor)
}
postEditor.cancelSnapshot();
postEditor.cancelSnapshot()
}

_restoreFromSnapshot(snapshot, postEditor) {
let { mobiledoc } = snapshot;
let { editor } = this;
let { builder, post } = editor;
let restoredPost = mobiledocParsers.parse(builder, mobiledoc);
let { mobiledoc } = snapshot
let { editor } = this
let { builder, post } = editor
let restoredPost = mobiledocParsers.parse(builder, mobiledoc)

postEditor.removeAllSections();
postEditor.migrateSectionsFromPost(restoredPost);
postEditor.removeAllSections()
postEditor.migrateSectionsFromPost(restoredPost)

// resurrect snapshotted range if it exists
let newRange = snapshot.getRange(post);
let newRange = snapshot.getRange(post)
if (newRange) {
postEditor.setRange(newRange);
postEditor.setRange(newRange)
}
}
}
Loading

0 comments on commit 83cd11c

Please sign in to comment.