-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Discover] move source field formatter to discover
- Loading branch information
Showing
13 changed files
with
168 additions
and
175 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
42 changes: 42 additions & 0 deletions
42
...lugins/discover/public/application/apps/main/components/doc_table/lib/formatters/mocks.ts
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,42 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { IndexPattern } from '../../../../../../../../../data/common'; | ||
import { stubbedSavedObjectIndexPattern } from '../../../../../../../../../data/common/stubs'; | ||
import { fieldFormatsMock } from '../../../../../../../../../field_formats/common/mocks'; | ||
import { DocTableRow } from '../../components/table_row'; | ||
|
||
export const hit = { | ||
_id: 'a', | ||
_type: 'doc', | ||
_score: 1, | ||
_source: { | ||
foo: 'bar', | ||
number: 42, | ||
hello: '<h1>World</h1>', | ||
also: 'with "quotes" or \'single quotes\'', | ||
}, | ||
} as DocTableRow; | ||
|
||
const createIndexPattern = () => { | ||
const id = 'my-index'; | ||
const { | ||
type, | ||
version, | ||
attributes: { timeFieldName, fields, title }, | ||
} = stubbedSavedObjectIndexPattern(id); | ||
|
||
return new IndexPattern({ | ||
spec: { id, type, version, timeFieldName, fields: JSON.parse(fields), title }, | ||
fieldFormats: fieldFormatsMock, | ||
shortDotsEnable: false, | ||
metaFields: [], | ||
}); | ||
}; | ||
|
||
export const indexPattern = createIndexPattern(); |
File renamed without changes.
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
40 changes: 40 additions & 0 deletions
40
...ublic/application/apps/main/components/doc_table/lib/formatters/source_formatter.test.tsx
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,40 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { formatSource } from './source_formatter'; | ||
import { hit, indexPattern } from './mocks'; | ||
|
||
describe('_source formatter', () => { | ||
it('should format properly', () => { | ||
const element = formatSource({ hit, indexPattern, isShortDots: true }); | ||
expect(element).toMatchInlineSnapshot(` | ||
<TemplateComponent | ||
defPairs={ | ||
Array [ | ||
Array [ | ||
"also", | ||
"with \\"quotes\\" or 'single quotes'", | ||
], | ||
Array [ | ||
"foo", | ||
"bar", | ||
], | ||
Array [ | ||
"hello", | ||
"<h1>World</h1>", | ||
], | ||
Array [ | ||
"number", | ||
42, | ||
], | ||
] | ||
} | ||
/> | ||
`); | ||
}); | ||
}); |
59 changes: 59 additions & 0 deletions
59
...ver/public/application/apps/main/components/doc_table/lib/formatters/source_formatter.tsx
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,59 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import React, { Fragment } from 'react'; | ||
import { escape } from 'lodash'; | ||
import { DocTableRow } from '../../components/table_row'; | ||
import { shortenDottedString } from '../../../../../../../../../field_formats/common'; | ||
import { IndexPattern } from '../../../../../../../../../data/public'; | ||
|
||
interface Props { | ||
defPairs: Array<[string, string]>; | ||
} | ||
|
||
const TemplateComponent = ({ defPairs }: Props) => { | ||
return ( | ||
<dl className={'source truncate-by-height'}> | ||
{defPairs.map((pair, idx) => ( | ||
<Fragment key={idx}> | ||
<dt | ||
dangerouslySetInnerHTML={{ __html: `${escape(pair[0])}:` }} // eslint-disable-line react/no-danger | ||
/> | ||
<dd | ||
dangerouslySetInnerHTML={{ __html: `${pair[1]}` }} // eslint-disable-line react/no-danger | ||
/>{' '} | ||
</Fragment> | ||
))} | ||
</dl> | ||
); | ||
}; | ||
|
||
export const formatSource = ({ | ||
hit, | ||
indexPattern, | ||
isShortDots, | ||
}: { | ||
hit: DocTableRow; | ||
indexPattern: IndexPattern; | ||
isShortDots: boolean; | ||
}) => { | ||
const highlights: Record<string, string[]> = (hit && hit.highlight) || {}; | ||
// TODO: remove index pattern dependency | ||
const formatted = hit ? indexPattern.formatHit(hit) : {}; | ||
const highlightPairs: Array<[string, string]> = []; | ||
const sourcePairs: Array<[string, string]> = []; | ||
|
||
Object.keys(formatted).forEach((key) => { | ||
const pairs = highlights[key] ? highlightPairs : sourcePairs; | ||
const newField = isShortDots ? shortenDottedString(key) : key; | ||
const val = formatted![key]; | ||
pairs.push([newField as string, val]); | ||
}, []); | ||
|
||
return <TemplateComponent defPairs={highlightPairs.concat(sourcePairs)} />; | ||
}; |
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
49 changes: 0 additions & 49 deletions
49
src/plugins/field_formats/common/converters/source.test.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.