Skip to content

Commit

Permalink
feat(studio): allow third party content types to preview images (#390)
Browse files Browse the repository at this point in the history
* feat(studio): allow third party content types to preview images

* add unit testing for recursiveSearch

* requested changes
  • Loading branch information
davidvitora authored Aug 24, 2022
1 parent c9efb46 commit d119641
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 5 deletions.
10 changes: 10 additions & 0 deletions jest.unit.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ const config: Config.InitialOptions = {
...tsjPreset.transform
},
clearMocks: true
},
{
rootDir: 'packages/studio-ui/src/web',
testMatch: ['<rootDir>/**/*.test.ts'],
displayName: { name: 'ui', color: 'blue' },
testEnvironment: 'jsdom',
transform: {
...tsjPreset.transform
},
clearMocks: true
}
]
}
Expand Down
16 changes: 16 additions & 0 deletions packages/studio-ui/src/web/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,19 @@ export const sanitizeName = (text: string) =>
export const stripDots = (str: string) => str.replace(/\./g, '--dot--')

export const restoreDots = (str: string) => str.replace(/--dot--/g, '.')

export const recursiveSearch = (obj: any, searchKey: string, results: string[] = []) => {
if (!obj || typeof obj !== 'object') {
return results
}

Object.keys(obj).forEach((key) => {
const value = obj[key]
if (key === searchKey && typeof value !== 'object') {
results.push(value)
} else if (typeof value === 'object') {
recursiveSearch(value, searchKey, results)
}
})
return results
}
25 changes: 25 additions & 0 deletions packages/studio-ui/src/web/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { recursiveSearch } from './util'

test('recursiveSearch', () => {
const obj = {
sub: {
id: 1,
type: 'image',
micro: {
name: 'test',
type: 'node'
}
},
id: 2,
type: 'standard'
}
expect(recursiveSearch(obj, 'type')).toEqual(['image', 'node', 'standard'])
expect(recursiveSearch(obj, 'other')).toEqual([])
expect(recursiveSearch({}, 'type')).toEqual([])
expect(recursiveSearch([], 'type')).toEqual([])
expect(recursiveSearch(null, 'type')).toEqual([])
expect(recursiveSearch(undefined, 'type')).toEqual([])
expect(recursiveSearch('string', 'type')).toEqual([])
expect(recursiveSearch(1, 'type')).toEqual([])
expect(recursiveSearch(obj, '')).toEqual([])
})
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { fetchContentItem, refreshFlowsLinks } from '~/actions'

import { isMissingCurlyBraceClosure } from '~/components/Util/form.util'
import { isRTLLocale } from '~/translations'
import { restoreDots, stripDots } from '~/util'
import { restoreDots, stripDots, recursiveSearch } from '~/util'
import withLanguage from '../../../components/Util/withLanguage'
import { ActionPopover } from './ActionPopover'

Expand Down Expand Up @@ -65,7 +65,9 @@ class ActionItem extends Component<Props> {
[style.missingTranslation]: preview?.startsWith('(missing translation) ')
})

if (preview && item?.schema?.title === 'image') {
const hasImageSubtype = recursiveSearch(item?.schema?.json, '$subtype')?.indexOf('image') !== -1

if (preview && hasImageSubtype) {
const markdownRender = (
<Markdown
source={preview}
Expand Down
6 changes: 3 additions & 3 deletions packages/studio-ui/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"target": "es5",
"lib": ["es7", "dom"],
"jsx": "react",
"typeRoots": ["./node_modules/@types", "src/web/typings.d.ts"],
"types": ["bluebird-global"],
"typeRoots": ["./node_modules/@types", "src/web/typings.d.ts", "../../node_modules/@types"],
"types": ["jest", "bluebird-global"],
"baseUrl": "./",
"paths": {
"~/*": ["src/web/*"],
Expand All @@ -21,5 +21,5 @@
}
},
"include": ["src/**/*.ts", "src/**/*.tsx"],
"exclude": []
"exclude": ["**/*.test.ts"]
}

0 comments on commit d119641

Please sign in to comment.