@lexical/react
Version:
This package provides Lexical components and hooks for React applications.
157 lines (140 loc) • 5.1 kB
JavaScript
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import { DecoratorNode, $getDocument, nodeSchema, withField, enumValue } from 'lexical';
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
// @generated by scripts/generate-node-json.mjs from each class's `json`
// schema. Run `pnpm run generate-node-json` to regenerate; do not edit.
// Keys are emitted in the order the schema-driven walk writes them, so the two
// produce byte-identical JSON. That order is the schema's, not alphabetical.
/* eslint-disable sort-keys-fix/sort-keys-fix */
/**
* DecoratorBlockNode's schema-declared fields, for a clone. Generated from that
* schema; do not edit by hand.
*
* @internal
*/
function afterCloneDecoratorBlockNode(node, prevNode) {
node.__format = prevNode.__format;
}
/** DecoratorBlockNode's generated implementations, for its `$config`. @internal */
const GENERATED_DECORATORBLOCK = () => {
/** Generated from DecoratorBlockNode's serialization schema. Do not edit by hand. */
function exportDecoratorBlockNode(node) {
return {
format: node.__format,
type: node.__type,
version: 1
};
}
/** Generated from DecoratorBlockNode's serialization schema. Do not edit by hand. */
function exportCompactDecoratorBlockNode(node) {
const json = {
type: node.__type
};
const format = node.__format;
if (format !== undefined && format !== '') {
json.format = format;
}
return json;
}
/** Generated from DecoratorBlockNode's serialization schema. Do not edit by hand. */
function updateDecoratorBlockNode(node, json) {
const format = json.format;
node.__format = format === '' || format === 'left' || format === 'start' || format === 'center' || format === 'right' || format === 'end' || format === 'justify' ? format : '';
return node;
}
return {
exportJSON: exportDecoratorBlockNode,
exportCompactJSON: exportCompactDecoratorBlockNode,
updateFromJSON: updateDecoratorBlockNode,
afterCloneFrom: afterCloneDecoratorBlockNode
};
};
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
/**
* The serialized form of a {@link DecoratorBlockNode}: the base serialized node
* data plus the block's element `format` (alignment).
*/
// Single source of truth for parsing the node-specific properties of a
// SerializedDecoratorBlockNode. DecoratorBlockNode is an abstract base (it has
// no concrete node type) so it publishes its schema on `$config` under the
// well-known `Symbol.for('DecoratorBlockNode')` key; concrete subclasses
// compose it with their own.
const decoratorBlockNodeSchema = /* @__PURE__ */nodeSchema()({
format: /* @__PURE__ */withField(/* @__PURE__ */enumValue(['', 'left', 'start', 'center', 'right', 'end', 'justify']), {
field: '__format'
})
});
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
/**
* A base class for block-level {@link DecoratorNode}s (decorator nodes rendered
* on their own line rather than inline). It stores an {@link ElementFormatType}
* alignment, is not indentable, and renders into a `<div>`. Extend it for custom
* block embeds such as images, videos, or tweets, typically pairing it with
* {@link BlockWithAlignableContents} to handle selection and alignment.
*/
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
class DecoratorBlockNode extends DecoratorNode {
__format;
constructor(format, key) {
super(key);
this.__format = format || '';
}
$config() {
return this.config(Symbol.for('DecoratorBlockNode'), {
// Named explicitly, as every abstract config in the tree is: this class
// carries the only declaration of `format` that its concrete subclasses
// inherit, and composeSchema honors an explicit `extends` where a
// severed static prototype chain (Babel's loose class transform) would
// otherwise stop the walk here and drop the property from both
// directions.
extends: DecoratorNode,
generated: GENERATED_DECORATORBLOCK,
json: decoratorBlockNodeSchema
});
}
canIndent() {
return false;
}
createDOM() {
return $getDocument().createElement('div');
}
updateDOM() {
return false;
}
setFormat(format) {
const self = this.getWritable();
self.__format = format;
return self;
}
getFormat() {
return this.getLatest().__format;
}
isInline() {
return false;
}
}
/**
* @returns `true` if `node` is a {@link DecoratorBlockNode}, narrowing its type.
*/
function $isDecoratorBlockNode(node) {
return node instanceof DecoratorBlockNode;
}
export { $isDecoratorBlockNode, DecoratorBlockNode };