-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: EditorLocalStoragePlugin (#640)
- Loading branch information
Showing
4 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { updateSpec, loadFromLocalStorage, download } from './wrap-actions.js'; | ||
|
||
/** | ||
* wraps updateSpec to also save specStr to localStorage | ||
* wraps download to check if it should be ignored | ||
*/ | ||
|
||
const EditorLocalStoragePlugin = (system) => { | ||
loadFromLocalStorage(system); // will check if exists | ||
return { | ||
statePlugins: { | ||
spec: { | ||
wrapActions: { | ||
updateSpec, | ||
download, | ||
}, | ||
}, | ||
}, | ||
}; | ||
}; | ||
|
||
export default EditorLocalStoragePlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const CONTENT_KEY = 'swagger-ide-content'; | ||
const { localStorage } = window; | ||
|
||
const saveContentToStorage = (str) => { | ||
return localStorage.setItem(CONTENT_KEY, str); | ||
}; | ||
|
||
export const updateSpec = (oriAction) => (specStr) => { | ||
oriAction(specStr); | ||
saveContentToStorage(specStr); | ||
}; | ||
|
||
export const loadFromLocalStorage = (system) => { | ||
// setTimeout runs on the next tick | ||
setTimeout(() => { | ||
if (localStorage.getItem(CONTENT_KEY)) { | ||
system?.specActions?.updateSpec(localStorage.getItem(CONTENT_KEY), 'local-storage'); | ||
} | ||
}, 0); | ||
}; | ||
|
||
/** | ||
* when given a SwaggerUI config prop `url`, | ||
* before `download` of new specStr as specified by `url`, | ||
* check if active specStr already exists in localStorage | ||
*/ | ||
export const download = (oriAction) => (specStr) => { | ||
if (!localStorage.getItem(CONTENT_KEY)) { | ||
oriAction(specStr); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
describe('EditorLocalStoragePlugin', () => { | ||
beforeEach(() => { | ||
cy.intercept( | ||
'GET', | ||
'https://raw.githubusercontent.com/asyncapi/spec/v2.2.0/examples/streetlights-kafka.yml', | ||
{ | ||
fixture: 'streetlights-kafka.yml', | ||
} | ||
).as('streetlightsKafka'); | ||
|
||
cy.visit('/', {}); | ||
// tests when initial URL is set to AsyncAPI streetlights-kafka.yml | ||
cy.wait('@streetlightsKafka').then(() => { | ||
// console.log('ok'); | ||
}); | ||
}); | ||
|
||
it('should load definition with provided url prop', () => { | ||
cy.get('.monaco-editor .view-lines') | ||
.should('contains.text', 'asyncapi') | ||
.should('contains.text', '2.2.0'); | ||
}); | ||
|
||
it('should reload while keeping text change from 2.2.0 to 2.1.0', () => { | ||
const moveToPosition = `{rightArrow}{rightArrow}{rightArrow}{rightArrow}{rightArrow}{rightArrow}{rightArrow}{rightArrow}{rightArrow}{rightArrow}{rightArrow}{rightArrow}{rightArrow}`; | ||
cy.get('.monaco-editor textarea:first') | ||
.click() | ||
.focused() | ||
.type(`${moveToPosition}{shift+rightArrow}1`); | ||
cy.get('.monaco-editor .view-lines').should('contains.text', '2.1.0'); | ||
cy.reload(); | ||
cy.get('.monaco-editor .view-lines') | ||
.should('contains.text', '2.1.0') | ||
.should('not.contains.text', '2.2.0'); | ||
}); | ||
}); |