Skip to content

Commit

Permalink
Merge pull request #249 from kommitters/v2.0
Browse files Browse the repository at this point in the history
Release v2.0.27
  • Loading branch information
MarioRodriguezS authored Apr 1, 2024
2 parents acbec63 + b51bf10 commit 2e620c2
Show file tree
Hide file tree
Showing 9 changed files with 2,454 additions and 2,648 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Harden Runner
uses: step-security/harden-runner@8ca2b8b2ece13480cda6dacd3511b49857a23c09 # v2.5.1
uses: step-security/harden-runner@63c24ba6bd7ba022e95695ff85de572c04a18142 # v2.7.0
with:
disable-sudo: true
egress-policy: block
Expand All @@ -23,14 +23,15 @@ jobs:
github.com:443
nodejs.org:443
registry.yarnpkg.com:443
objects.githubusercontent.com:443
- uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
- uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version: 16.x
- run: yarn
- run: yarn test --coverage
- name: Coveralls
uses: coverallsapp/github-action@66741573064b23a82206aebd518f4bb11802fa5a
uses: coverallsapp/github-action@3dfc5567390f6fa9267c0ee9c251e4c8c3f18949 # v2.2.3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
10 changes: 5 additions & 5 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:

steps:
- name: Harden Runner
uses: step-security/harden-runner@8ca2b8b2ece13480cda6dacd3511b49857a23c09 # v2.5.1
uses: step-security/harden-runner@63c24ba6bd7ba022e95695ff85de572c04a18142 # v2.7.0
with:
disable-sudo: true
egress-policy: block
Expand All @@ -52,11 +52,11 @@ jobs:
*.githubusercontent.com:443
- name: Checkout repository
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@7df0ce34898d659f95c0c4a09eaa8d4e32ee64db # v2.2.12
uses: github/codeql-action/init@1b1aada464948af03b950897e5eb522f92603cc2 # v3.24.9
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -69,7 +69,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@7df0ce34898d659f95c0c4a09eaa8d4e32ee64db # v2.2.12
uses: github/codeql-action/autobuild@1b1aada464948af03b950897e5eb522f92603cc2 # v3.24.9

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
Expand All @@ -82,6 +82,6 @@ jobs:
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@7df0ce34898d659f95c0c4a09eaa8d4e32ee64db # v2.2.12
uses: github/codeql-action/analyze@1b1aada464948af03b950897e5eb522f92603cc2 # v3.24.9
with:
category: "/language:${{matrix.language}}"
6 changes: 3 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Harden Runner
uses: step-security/harden-runner@8ca2b8b2ece13480cda6dacd3511b49857a23c09 # v2.5.1
uses: step-security/harden-runner@63c24ba6bd7ba022e95695ff85de572c04a18142 # v2.7.0
with:
disable-sudo: true
egress-policy: block
Expand All @@ -19,9 +19,9 @@ jobs:
registry.npmjs.org:443
registry.yarnpkg.com:443
- uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
- uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
with:
node-version: "16.x"
registry-url: "https://registry.npmjs.org"
Expand Down
18 changes: 9 additions & 9 deletions .github/workflows/scorecards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:

steps:
- name: Harden Runner
uses: step-security/harden-runner@8ca2b8b2ece13480cda6dacd3511b49857a23c09 # v2.5.1
uses: step-security/harden-runner@63c24ba6bd7ba022e95695ff85de572c04a18142 # v2.7.0
with:
disable-sudo: true
egress-policy: block
Expand All @@ -31,19 +31,19 @@ jobs:
api.osv.dev:443
api.securityscorecards.dev:443
bestpractices.coreinfrastructure.org:443
fulcio.sigstore.dev:443
*.sigstore.dev:443
github.com:443
rekor.sigstore.dev:443
bestpractices.dev:443
*.storage.googleapis.com:443
sigstore-tuf-root.storage.googleapis.com:443
oss-fuzz-build-logs.storage.googleapis.com:443
www.bestpractices.dev:443
- name: "Checkout code"
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
persist-credentials: false

- name: "Run analysis"
uses: ossf/scorecard-action@80e868c13c90f172d68d1f4501dee99e2479f7af # v2.1.3
uses: ossf/scorecard-action@0864cf19026789058feabb7e87baa5f140aac736 # v2.3.1
with:
results_file: results.sarif
results_format: sarif
Expand All @@ -62,14 +62,14 @@ jobs:
# Upload the results as artifacts (optional). Commenting out will disable uploads of run results in SARIF
# format to the repository Actions tab.
- name: "Upload artifact"
uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # v3.1.2
uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # v4.3.1
with:
name: SARIF file
path: results.sarif
retention-days: 5

# Upload the results to GitHub's code scanning dashboard.
- name: "Upload to code-scanning"
uses: github/codeql-action/upload-sarif@7df0ce34898d659f95c0c4a09eaa8d4e32ee64db # v2.2.12
uses: github/codeql-action/upload-sarif@1b1aada464948af03b950897e5eb522f92603cc2 # v3.24.9
with:
sarif_file: results.sarif
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 2.0.27 (01.04.2024)

- Fix bug [#243](https://github.com/kommitters/editorjs-undo/issues/243) - Fix unexpected behavior when there are initial data loaded.
- Fix bug [#243](https://github.com/kommitters/editorjs-undo/issues/243) - Fix unexpected behavior when there are several copied paragraphs.
- Update [#206](https://github.com/kommitters/editorjs-undo/pull/206) - Update Github workflows dependencies and allowed-endpoints.
- Fix bug [#224](https://github.com/kommitters/editorjs-undo/pull/244) - Fix unexpected behavior when trying to split and create a new paragraph.

## 2.0.26 (25.10.2023)

- Fix bug [#238](https://github.com/kommitters/editorjs-undo/issues/238) - Fix bug in v2.0.25 related to runtime handle.
Expand Down
3 changes: 2 additions & 1 deletion dist/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "editorjs-undo",
"version": "2.0.26",
"version": "2.0.27",
"keywords": [
"undo",
"redo",
Expand Down
72 changes: 50 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export default class Undo {
}

/**
* returns true if readOnly was toggled to true
* Returns true if readOnly was toggled to true
* @returns {Node} Indirectly shows if readOnly was set to true or false
*/
setReadOnly() {
Expand Down Expand Up @@ -177,7 +177,7 @@ export default class Undo {
}

/**
* get the caret position.
* Gets the caret position.
* @param {Number} index is the block index
* @returns The caret position
*/
Expand All @@ -189,7 +189,7 @@ export default class Undo {
}

/**
* insert a block deleted previously
* Inserts a block deleted previously
* @param {Array} state is the current state according to this.position.
* @param {Array} compState is the state to compare and know the deleted block.
* @param {Number} index is the block index in state.
Expand All @@ -205,7 +205,7 @@ export default class Undo {
}

/**
* return true if a block was dropped previously
* Returns true if a block was dropped previously
* @param {Array} state is the current state according to this.position.
* @param {Array} compState is the state to compare and know the dropped block.
* @returns {Boolean} true if the block was dropped
Expand All @@ -218,19 +218,17 @@ export default class Undo {
}

/**
* return true if the block has to be deleted becuase it was skipped previously.
* @param {Number} index is the block index in state.
* @param {Number} compIndex is the index to compare and know if the block was inserted previously
* Returns true if the block has to be deleted because it was skipped previously.
* @param {Array} state is the current state according to this.position.
* @param {Array} compState is the stato to compare if there was a deleted block.
* @param {Array} compState is the state to compare if there was a deleted block.
* @returns {Boolean} true if a block was inserted previously.
*/
blockWasSkipped(index, compIndex, state, compState) {
return index < compIndex && state.length !== compState.length;
blockWasSkipped(state, compState) {
return state.length !== compState.length;
}

/**
* return true if the content in a block without the focus was modified.
* Returns true if the content in a block without the focus was modified.
* @param {Number} index is the block index in state.
* @param {Number} compIndex is the index to compare and know if the block was inserted previously
* @returns true if the content in a block without the focus was modified.
Expand All @@ -240,7 +238,7 @@ export default class Undo {
}

/**
* returns true if a block was deleted previously.
* Returns true if a block was deleted previously.
* @param {Array} state is the current state according to this.position.
* @param {Array} compState is the state to compare and know if a block was deleted.
* @returns {Boolean} true if a block was deleted previously.
Expand All @@ -249,6 +247,18 @@ export default class Undo {
return state.length > compState.length;
}

/**
* Returns true if the content was copied.
* @param {Array} state is the current state according to this.position.
* @param {Array} compState is the state to compare and know if the content was copied.
* @param {Number} index is the block index in state.
* @returns {Boolean} true if a block was deleted previously.
*/
contentWasCopied(state, compState, index) {
return Object.keys(state[index].data).length === 0
&& JSON.stringify(compState[index + 1]) !== JSON.stringify(state[index + 1]);
}

/**
* Decreases the current position and update the respective block in the editor.
*/
Expand All @@ -271,12 +281,14 @@ export default class Undo {

if (this.blockWasDeleted(state, nextState)) {
this.insertDeletedBlock(state, nextState, index);
} else if (this.blockWasSkipped(index, nextIndex, state, nextState)) {
} else if (this.contentWasCopied(state, nextState, index)) {
await this.blocks.render({ blocks: state });
this.caret.setToBlock(index, 'end');
} else if (index < nextIndex && this.blockWasSkipped(state, nextState)) {
await this.blocks.delete(nextIndex);
this.caret.setToBlock(index, "end");
} else if (blockCount > state.length) {
await this.blocks.render({ blocks: state });
await this.editor.blocks.insert(this.defaultBlock, {});
this.setCaretIndex(index, caretIndex);
} else if (this.blockWasDropped(state, nextState)) {
await this.blocks.render({ blocks: state });
Expand All @@ -297,7 +309,7 @@ export default class Undo {
}

/**
* Set the caret position.
* Sets the caret position.
* @param {Number} index is the block index
* @param {Number} caretIndex is the caret position
* @param {Array} state is the current state according to this.position.
Expand All @@ -313,7 +325,7 @@ export default class Undo {
}

/**
* Insert new block
* Inserts new block
* @param {Array} state is the current state according to this.position.
* @param {Number} index is the block index
*/
Expand All @@ -328,14 +340,30 @@ export default class Undo {
}

/**
* Insert a block when is skipped
* @param {Number} prevStateLength is the previous state according to this.position.
* Inserts a block when is skipped and update the previous one if it changed.
* @param {Array} prevState is the previous state according to this.position.
* @param {Array} state is the current state according to this.position.
* @param {Number} index is the block index.
*/
insertSkippedBlocks(prevStateLength, state) {
for (let i = prevStateLength; i < state.length; i += 1) {
async insertSkippedBlocks(prevState, state, index) {
for (let i = prevState.length; i < state.length; i += 1) {
this.insertBlock(state, i);
}

if (JSON.stringify(prevState[index - 1]) !== JSON.stringify(state[index - 1])) {
await this.updateModifiedBlock(state, index);
}
}

/**
* Updates the passed block or render the state when the content was copied.
* @param {Array} state is the current state according to this.position.
* @param {Number} index is the block index.
*/
async updateModifiedBlock(state, index) {
const block = state[index - 1];
if (this.editor.blocks.getById(block.id)) return this.blocks.update(block.id, block.data);
return this.blocks.render({ blocks: state });
}

/**
Expand All @@ -352,8 +380,8 @@ export default class Undo {
if (this.blockWasDeleted(prevState, state)) {
await this.blocks.delete();
this.caret.setToBlock(index, "end");
} else if (this.blockWasSkipped(prevIndex, index, state, prevState)) {
this.insertSkippedBlocks(prevState.length, state);
} else if (this.blockWasSkipped(state, prevState)) {
await this.insertSkippedBlocks(prevState, state, index);
this.caret.setToBlock(index, 'end');
} else if (this.blockWasDropped(state, prevState) && this.position !== 1) {
await this.blocks.render({ blocks: state });
Expand Down
Loading

0 comments on commit 2e620c2

Please sign in to comment.