@liferay/js-api
v1.0.0
Published
Public Liferay DXP API to be used from Client Extensions.
Maintainers
Keywords
Readme
@liferay/js-api
Public Liferay DXP API to be used from Client Extensions.
This package exposes the stable, public TypeScript contracts that a Client Extension uses to integrate with Liferay DXP features.
Installation
npm install @liferay/js-apiModules
@liferay/js-api/data-set
Public type contracts for the Frontend Data Set (FDS) widget. The module exposes only types; it contains no implementation. The contracts are split by functionality across sibling modules and re-exported from the module entry point, covering four groups:
- FDS connection and remote state (
./connection) —FDSConnection(and its companionFDSConnectionConstructor) let a Client Extension read and write FDS search state and take the data set's filtering over withFDSConnectionFilterexpressions and oneFDSConnectionCustomConfigthe data set keeps in the page URL, whileFDSConnectionInfo,FDSConnectionStatus,FDSConnectionOptions,FDSConnectionOwnership,FDSState, andFDSStateChangeCallbackdescribe how a connection is opened and observed. - Custom cell renderers (
./cell-renderer) —FDSTableCellHTMLElementBuilderand its args, the HTML element builder a renderer implements to draw a table cell. - Custom filters (
./filter) —FDSFilterand the HTML element builders (rendering), OData query builders (server-side filtering), and description builders (human-readable filter summaries) it composes. - Custom visualization modes (
./visualization-mode) —FDSVisualizationMode, the factory a Client Extension exports to show the items of a data set its own way, theFDSVisualizationModeInstanceit returns, whoseupdate()draws them, and theFDSVisualizationModeArgsthe data set hands it, with itsFDSVisualizationModeSchemafield mapping andFDSVisualizationModeSelection.
import type {
FDSTableCellHTMLElementBuilder,
FDSFilter,
} from '@liferay/js-api/data-set';Custom visualization modes
A visualization mode Client Extension draws the items of a data set next to the table, list and cards it ships with. The data set owns the data and the Client Extension owns the drawing: the data set searches, filters, sorts and paginates, then hands the current page of items to the visualization mode, which draws them into the container it is given with plain DOM. No framework crosses the boundary, so the Client Extension may use any or none.
The default export is a factory, called once per instance with an empty container. It sets the instance up, such as creating the elements the items go in, and returns the instance the data set drives:
update(args)draws. It runs right after the factory returns, and again whenever what the data set handed over changes while the visualization mode stays visible, such as the selection. It receives the full arguments, not what changed, so the same function draws the first page and every change after it.destroy()runs before the data set takes the visualization mode away. Release whatever the instance holds outside its container; the data set empties the container itself.
Reloading data, which every search, filter, sort and page change does, destroys the instance and creates a new one, so nothing kept on it outlives a reload. A page may also show the same visualization mode in several data sets at once, so keep state on the instance rather than in the module.
Read item fields through schema rather than by fixed names: the data set
decides which field fills each part the visualization mode draws, so the same
visualization mode works across data sets whose items name their fields
differently. selection is present only when the data set lets its items be
selected; toggleItem() asks the data set to change the selection, which then
calls update() with the new selectedValues.
import type {
FDSVisualizationMode,
FDSVisualizationModeArgs,
} from '@liferay/js-api/data-set';
const visualizationMode: FDSVisualizationMode = (container) => {
const list = document.createElement('ul');
container.append(list);
const draw = ({items, schema}: FDSVisualizationModeArgs) => {
list.replaceChildren(
...items.map((item) => {
const entry = document.createElement('li');
entry.textContent = String(item[schema?.title ?? 'title']);
return entry;
})
);
};
return {destroy: () => {}, update: draw};
};
export default visualizationMode;@liferay/js-api/data-set/connection
The typed surface of the Frontend Data Set runtime client. Unlike the
data-set entry point, which re-exports only types, this module also declares
FDSConnection as a value (the constructor) alongside its type (the
instance), so consumers can new FDSConnection(...) and annotate with
FDSConnection exactly as they would a class.
At runtime the FDS connection is implemented and served by the portal as the
@liferay/frontend-data-set-web/api ES module and resolved through the import
map. A Client Extension redirects that import-map specifier to this module at
build time (via a tsconfig paths entry) so it can construct a connection
with full typing while the value is pulled from the import-map module at
runtime.
import {FDSConnection} from '@liferay/js-api/data-set/connection';
const connection = new FDSConnection(
fdsName,
{
search: (query) => {
/* ... */
},
},
(info) => {
/* ... */
},
{timeout: 5000}
);Taking over the filtering
A connection drives the search and nothing else by default. Declaring
owns: ['filters'] takes the data set's filtering over as well: the filters
it declares stop reaching the request, it drops its filters dropdown and its
filter chips, and the Client Extension owns the whole expression through
setFilters().
A data set has one filtering owner and the first connection to ask for it gets
it, so asking is not getting. A refused connection settles at the refused
status rather than ready and drives the search alone, which a Client
Extension that enables its controls once ready handles without writing any
code for the case.
Owning the filtering requires an appId: whatever the Client Extension asks
the data set to remember is filed in the page URL under it, and handed back
through the apply callback on the next visit or when the browser's back
button lands on a different address.
Call disconnect() when the Client Extension goes, from the custom element's
disconnectedCallback or its framework's equivalent. A connection that is
never disconnected keeps the filtering of that data set claimed, and nothing
on the page can filter it again until the next full page load.
const connection = new FDSConnection(
fdsName,
{
apply: (customConfig) => {
// Nothing has validated this: it comes from a URL anyone can edit.
const selection = parseSelection(customConfig);
drawFilterUI(selection);
connection.setFilters(toODataFilters(selection), selection);
},
search: (query) => {
/* ... */
},
},
({status}) => {
setControlsEnabled(status === 'ready');
},
{appId: 'mySampleFilterApp', owns: ['filters']}
);@liferay/js-api/editor
Types for editor transformers. EditorTransformer and
EditorConfigTransformer let a Client Extension transform an editor's
configuration. For CKEditor 4, see the
config API.
import type {EditorTransformer} from '@liferay/js-api/editor';This module only describes DXP's editor contracts; it does not re-export any
CKEditor 5 plugin. A Client Extension that needs one (for example
@ckeditor/ckeditor5-word-count)
should depend on that package directly, pinned to the version the portal ships,
and mark it as external in its build so the portal's import map serves it at
runtime:
import {WordCount} from '@ckeditor/ckeditor5-word-count';
import type {EditorTransformer} from '@liferay/js-api/editor';
const transformer: EditorTransformer<EditorConfig> = {
editorConfigTransformer: (config) => ({
...config,
plugins: [...(config.plugins ?? []), WordCount],
}),
};Scripts
yarn build— compile the TypeScript sources withtsc.yarn clean— remove generated.d.ts/.jsoutput and build info.yarn test— run the test suite.yarn lint/yarn lint:fix— lint the sources.yarn format/yarn format:check— format the sources.
