@team-monolith/cds 1.47.0 → 1.48.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -30,11 +30,11 @@ export const light = {
30
30
  success: COLOR.green07,
31
31
  successActive: COLOR.green08,
32
32
  successDisabled: COLOR.green03,
33
- info: COLOR.teal08,
34
- infoActive: COLOR.teal09,
33
+ info: COLOR.teal06,
34
+ infoActive: COLOR.teal08,
35
35
  infoDisabled: COLOR.teal02,
36
- warning: COLOR.yellow08,
37
- warningActive: COLOR.yellow09,
36
+ warning: COLOR.yellow06,
37
+ warningActive: COLOR.yellow08,
38
38
  warningDisabled: COLOR.yellow02,
39
39
  inverse: COLOR.black,
40
40
  inverseActive: COLOR.grey08,
@@ -141,11 +141,11 @@ export const dark = {
141
141
  success: COLOR.green04,
142
142
  successActive: COLOR.green03,
143
143
  successDisabled: COLOR.green08,
144
- info: COLOR.teal03,
145
- infoActive: COLOR.teal02,
146
- infoDisabled: COLOR.teal09,
147
- warning: COLOR.yellow03,
148
- warningActive: COLOR.yellow02,
144
+ info: COLOR.teal06,
145
+ infoActive: COLOR.teal08,
146
+ infoDisabled: COLOR.teal02,
147
+ warning: COLOR.yellow08,
148
+ warningActive: COLOR.yellow06,
149
149
  warningDisabled: COLOR.yellow09,
150
150
  inverse: COLOR.white,
151
151
  inverseActive: COLOR.grey03,
@@ -1,6 +1,7 @@
1
1
  /// <reference types="react" />
2
2
  export type LexicalCustomConfig = {
3
3
  freezeProblemNode: boolean;
4
+ showQuizSolution: boolean;
4
5
  };
5
6
  declare const LexicalCustomConfigContext: import("react").Context<LexicalCustomConfig>;
6
7
  export { LexicalCustomConfigContext };
@@ -1,5 +1,6 @@
1
1
  import { createContext } from "react";
2
2
  const LexicalCustomConfigContext = createContext({
3
3
  freezeProblemNode: false,
4
+ showQuizSolution: false,
4
5
  });
5
6
  export { LexicalCustomConfigContext };
@@ -12,6 +12,8 @@ export interface LexicalEditorProps {
12
12
  /** freeze. 문제 블록을 수정하지 못하도록 함
13
13
  */
14
14
  freezeProblemNode?: boolean;
15
+ /** 퀴즈 정답이 공개되는지 여부. false가 default입니다. 공개하고자 하는곳에서 true로 설정해야 합니다. */
16
+ showQuizSolution?: boolean;
15
17
  /** 퀴즈 노드가 허용되는지 여부. false가 디폴트이고, 전달하지 않아도 로컬스토리지 devMode로도 노출시킬 수 있습니다. */
16
18
  isQuizEnabled?: boolean;
17
19
  /** 외부에서 플러그인을 주입하는 경우 활용함 */
@@ -13,8 +13,6 @@ import Plugins from "./Plugins";
13
13
  import { ColoredQuoteNode, ProblemInputNode } from "./nodes";
14
14
  import { LexicalCustomConfigContext } from "./LexicalCustomConfigContext";
15
15
  import { ProblemSelectNode } from "./nodes/ProblemSelectNode";
16
- import { LayoutContainerNode } from "./nodes/LayoutContainerNode";
17
- import { LayoutItemNode } from "./nodes/LayoutItemNode";
18
16
  function validateValue(value) {
19
17
  var _a, _b;
20
18
  if (value && typeof value !== "object") {
@@ -32,7 +30,7 @@ function validateValue(value) {
32
30
  return true;
33
31
  }
34
32
  export function LexicalEditor(props) {
35
- const { className, contentEditableClassName, value, onChange, editable = true, freezeProblemNode, isQuizEnabled = false, children, } = props;
33
+ const { className, contentEditableClassName, value, onChange, editable = true, showQuizSolution = false, freezeProblemNode = false, isQuizEnabled = false, children, } = props;
36
34
  const theme = useTheme();
37
35
  const initialConfig = {
38
36
  namespace: "CodleLexicalEditor",
@@ -54,8 +52,6 @@ export function LexicalEditor(props) {
54
52
  ImageNode,
55
53
  HorizontalRuleNode,
56
54
  ColoredQuoteNode,
57
- LayoutContainerNode,
58
- LayoutItemNode,
59
55
  {
60
56
  replace: QuoteNode,
61
57
  with: (_node) => {
@@ -67,5 +63,5 @@ export function LexicalEditor(props) {
67
63
  editorState: validateValue(value) ? JSON.stringify(value) : undefined,
68
64
  editable: editable,
69
65
  };
70
- return (_jsx(LexicalCustomConfigContext.Provider, Object.assign({ value: { freezeProblemNode: freezeProblemNode !== null && freezeProblemNode !== void 0 ? freezeProblemNode : false } }, { children: _jsxs(LexicalComposer, Object.assign({ initialConfig: initialConfig }, { children: [_jsx(Plugins, { className: className, contentEditableClassName: contentEditableClassName, onChange: onChange, isQuizEnabled: isQuizEnabled }), _jsx(_Fragment, { children: children })] })) })));
66
+ return (_jsx(LexicalCustomConfigContext.Provider, Object.assign({ value: { freezeProblemNode, showQuizSolution } }, { children: _jsxs(LexicalComposer, Object.assign({ initialConfig: initialConfig }, { children: [_jsx(Plugins, { className: className, contentEditableClassName: contentEditableClassName, onChange: onChange, isQuizEnabled: isQuizEnabled }), _jsx(_Fragment, { children: children })] })) })));
71
67
  }
@@ -29,7 +29,6 @@ import ListMaxIndentLevelPlugin from "./plugins/ListMaxIndentLevelPlugin";
29
29
  import styled from "@emotion/styled";
30
30
  import ProblemInputPlugin from "./plugins/ProblemInputPlugin";
31
31
  import ProblemSelectPlugin from "./plugins/ProblemSelectPlugin";
32
- import { LayoutPlugin } from "./plugins/LayoutPlugin/LayoutPlugin";
33
32
  export default function Plugins(props) {
34
33
  const { className, contentEditableClassName, onChange, isQuizEnabled } = props;
35
34
  const isEditable = useLexicalEditable();
@@ -44,7 +43,7 @@ export default function Plugins(props) {
44
43
  onChange === null || onChange === void 0 ? void 0 : onChange(editorState.toJSON());
45
44
  },
46
45
  // ignore 하지 않으면 Form에서 수정하지 않았는데 Dirty로 처리됨.
47
- ignoreSelectionChange: true }), _jsx(AutoFocusPlugin, {}), isEditable && (_jsxs(_Fragment, { children: [_jsx(TabIndentationPlugin, {}), _jsx(ComponentPickerMenuPlugin, { isQuizEnabled: isQuizEnabled }), _jsx(MarkdownShortcutPlugin, { transformers: CODLE_TRANSFORMERS }), _jsx(HistoryPlugin, {})] })), floatingAnchorElem && isEditable && (_jsxs(_Fragment, { children: [_jsx(ComponentAdderPlugin, { anchorElem: floatingAnchorElem, isQuizEnabled: isQuizEnabled }), _jsx(FloatingTextFormatToolbarPlugin, { anchorElem: floatingAnchorElem }), _jsx(FloatingLinkEditorPlugin, { anchorElem: floatingAnchorElem, isLinkEditMode: isLinkEditMode, setIsLinkEditMode: setIsLinkEditMode })] })), !isEditable && _jsx(LexicalClickableLinkPlugin, {}), _jsx(ListPlugin, {}), _jsx(HorizontalRulePlugin, {}), _jsx(ImagesPlugin, {}), _jsx(TablePlugin, {}), _jsx(LinkPlugin, {}), _jsx(ListMaxIndentLevelPlugin, { maxDepth: 5 }), _jsx(ProblemInputPlugin, {}), _jsx(ProblemSelectPlugin, {}), _jsx(LayoutPlugin, {})] }));
46
+ ignoreSelectionChange: true }), _jsx(AutoFocusPlugin, {}), isEditable && (_jsxs(_Fragment, { children: [_jsx(TabIndentationPlugin, {}), _jsx(ComponentPickerMenuPlugin, { isQuizEnabled: isQuizEnabled }), _jsx(MarkdownShortcutPlugin, { transformers: CODLE_TRANSFORMERS }), _jsx(HistoryPlugin, {})] })), floatingAnchorElem && isEditable && (_jsxs(_Fragment, { children: [_jsx(ComponentAdderPlugin, { anchorElem: floatingAnchorElem, isQuizEnabled: isQuizEnabled }), _jsx(FloatingTextFormatToolbarPlugin, { anchorElem: floatingAnchorElem }), _jsx(FloatingLinkEditorPlugin, { anchorElem: floatingAnchorElem, isLinkEditMode: isLinkEditMode, setIsLinkEditMode: setIsLinkEditMode })] })), !isEditable && _jsx(LexicalClickableLinkPlugin, {}), _jsx(ListPlugin, {}), _jsx(HorizontalRulePlugin, {}), _jsx(ImagesPlugin, {}), _jsx(TablePlugin, {}), _jsx(LinkPlugin, {}), _jsx(ListMaxIndentLevelPlugin, { maxDepth: 5 }), _jsx(ProblemInputPlugin, {}), _jsx(ProblemSelectPlugin, {})] }));
48
47
  }
49
48
  const ScrollArea = styled.div `
50
49
  min-height: 150px;
@@ -35,7 +35,7 @@ export function InputComponent(props) {
35
35
  // SoT를 내부 State로 관리하고 EditorState를 따로 설정.
36
36
  // 문제를 더 파악하고 추후 개선 필요.
37
37
  const [answerInput, setAnswerInput] = useState(answer);
38
- const lexicalCustomConfig = useContext(LexicalCustomConfigContext);
38
+ const { freezeProblemNode } = useContext(LexicalCustomConfigContext);
39
39
  // "** ***" 같은 형태입니다.
40
40
  const answerFormat = solutions[0].value;
41
41
  const handleChange = (value) => {
@@ -51,9 +51,9 @@ export function InputComponent(props) {
51
51
  // 학생 view
52
52
  if (!isEditable) {
53
53
  if (showCharacterNumber) {
54
- return (_jsx(SegmentedInput, { readOnly: lexicalCustomConfig.freezeProblemNode, isCorrect: isCorrect, answerFormat: answerFormat, placeholder: placeholder || "여기에 입력하세요.", value: answerInput, onChange: handleChange }));
54
+ return (_jsx(SegmentedInput, { readOnly: freezeProblemNode, isCorrect: isCorrect, answerFormat: answerFormat, placeholder: placeholder || "여기에 입력하세요.", value: answerInput, onChange: handleChange }));
55
55
  }
56
- return (_jsx(TextInput, { readOnly: lexicalCustomConfig.freezeProblemNode, isCorrect: isCorrect, size: "small", color: "default", placeholder: placeholder || "여기에 입력하세요.", value: answerInput, onChange: (e) => {
56
+ return (_jsx(TextInput, { readOnly: freezeProblemNode, isCorrect: isCorrect, size: "small", color: "default", placeholder: placeholder || "여기에 입력하세요.", value: answerInput, onChange: (e) => {
57
57
  handleChange(e.target.value);
58
58
  }, fullWidth: true }));
59
59
  }
@@ -29,9 +29,7 @@ export function SelectBoxView(props) {
29
29
  : undefined;
30
30
  return (_jsx(SelectBoxComponent, { css: css `
31
31
  width: 100%;
32
- `, type: selectBoxType, index: selectBoxType ? TYPE_TO_INDEX_ICON(selectBoxType) : index, image: image, text: text,
33
- // isAnswer가 존재하면 채점 후의 상태이므로 unclickable을 위해 onClick을 전달하지 않음
34
- onClick: isAnswer === undefined ? onClick : undefined,
32
+ `, type: selectBoxType, index: selectBoxType ? TYPE_TO_INDEX_ICON(selectBoxType) : index, image: image, text: text, onClick: onClick,
35
33
  // 선택되지 않았으나 정답일 때 정답 태그를 표시
36
34
  endIcon: isAnswer &&
37
35
  !isSelected && (_jsx(Tag, { label: "\uC815\uB2F5", icon: _jsx(CheckboxCircleFillIcon, {}), color: "green", css: css `
@@ -29,40 +29,40 @@ export function SelectComponent(props) {
29
29
  const [editor] = useLexicalComposerContext();
30
30
  const [settingOpen, setSettingOpen] = useState(false);
31
31
  const isEditable = useLexicalEditable();
32
- const lexicalCustomConfig = useContext(LexicalCustomConfigContext);
33
- // 학생 view
32
+ const { freezeProblemNode, showQuizSolution } = useContext(LexicalCustomConfigContext);
33
+ // view
34
34
  if (!isEditable) {
35
35
  return (_jsxs(_Fragment, { children: [hasMultipleAnswers && (_jsxs(Alert, { children: [_jsx(AlarmWarningFillIcon, { css: css `
36
36
  width: 14px;
37
37
  height: 14px;
38
- ` }), "\uC9C8\uBB38\uC5D0 \uD574\uB2F9\uD558\uB294 \uB2F5\uC744 \uBAA8\uB450 \uACE0\uB974\uB294 \uBB38\uC81C\uC785\uB2C8\uB2E4."] })), selections.map((selection, index) => (_jsx(SelectBoxView, { index: index + 1, isAnswer: selection.isAnswer, isSelected: selected.includes(selection.value), image: selection.show.image, text: selection.show.text, onClick: () => {
39
- if (lexicalCustomConfig.freezeProblemNode)
40
- return;
41
- const isSelected = selected.includes(selection.value);
42
- if (isSelected) {
43
- editor.update(() => {
44
- const node = $getNodeByKey(nodeKey);
45
- if (!$isProblemSelectNode(node)) {
46
- return;
47
- }
48
- node.setSelected(selected.filter((v) => v !== selection.value));
49
- });
50
- }
51
- else {
52
- editor.update(() => {
53
- const node = $getNodeByKey(nodeKey);
54
- if (!$isProblemSelectNode(node)) {
55
- return;
56
- }
57
- if (hasMultipleAnswers) {
58
- node.setSelected([...selected, selection.value]);
59
- }
60
- else {
61
- node.setSelected([selection.value]);
62
- }
63
- });
64
- }
65
- } }, index)))] }));
38
+ ` }), "\uC9C8\uBB38\uC5D0 \uD574\uB2F9\uD558\uB294 \uB2F5\uC744 \uBAA8\uB450 \uACE0\uB974\uB294 \uBB38\uC81C\uC785\uB2C8\uB2E4."] })), selections.map((selection, index) => (_jsx(SelectBoxView, { index: index + 1, isAnswer: showQuizSolution ? selection.isAnswer : undefined, isSelected: selected.includes(selection.value), image: selection.show.image, text: selection.show.text, onClick: freezeProblemNode
39
+ ? undefined
40
+ : () => {
41
+ const isSelected = selected.includes(selection.value);
42
+ if (isSelected) {
43
+ editor.update(() => {
44
+ const node = $getNodeByKey(nodeKey);
45
+ if (!$isProblemSelectNode(node)) {
46
+ return;
47
+ }
48
+ node.setSelected(selected.filter((v) => v !== selection.value));
49
+ });
50
+ }
51
+ else {
52
+ editor.update(() => {
53
+ const node = $getNodeByKey(nodeKey);
54
+ if (!$isProblemSelectNode(node)) {
55
+ return;
56
+ }
57
+ if (hasMultipleAnswers) {
58
+ node.setSelected([...selected, selection.value]);
59
+ }
60
+ else {
61
+ node.setSelected([selection.value]);
62
+ }
63
+ });
64
+ }
65
+ } }, index)))] }));
66
66
  }
67
67
  // 교사 edit view
68
68
  return (_jsxs(_Fragment, { children: [_jsxs("div", Object.assign({ css: css `
@@ -26,7 +26,6 @@ import { ZINDEX } from "../../../../utils/zIndex";
26
26
  import { css, useTheme } from "@emotion/react";
27
27
  import { INSERT_PROBLEM_INPUT_COMMAND } from "../ProblemInputPlugin";
28
28
  import { INSERT_PROBLEM_SELECT_COMMAND } from "../ProblemSelectPlugin";
29
- import { INSERT_LAYOUT_COMMAND } from "../LayoutPlugin/LayoutPlugin";
30
29
  // import useModal from "../../hooks/useModal";
31
30
  // import catTypingGif from "../../images/cat-typing.gif";
32
31
  // import { INSERT_IMAGE_COMMAND, InsertImageDialog } from "../ImagesPlugin";
@@ -197,17 +196,8 @@ export function getBaseOptions(props) {
197
196
  onSelect: () => setImageOpen(true),
198
197
  }),
199
198
  ];
200
- // 로컬스토리지 devMode가 true이면 칼럼 나누기 컨텍스트 메뉴를 추가합니다.
201
- const isDevMode = localStorage.getItem("devMode") === "true";
202
- if (isDevMode) {
203
- baseOptions.push(new ComponentPickerOption("칼럼 나누기", {
204
- icon: _jsx("i", { className: "icon columns" }),
205
- keywords: ["columns", "layout", "grid"],
206
- onSelect: () => editor.dispatchCommand(INSERT_LAYOUT_COMMAND, "1fr 1fr"),
207
- }));
208
- }
209
199
  // isQuizEnabled이거나 로컬스토리지 devMode가 true이면 퀴즈 컨텍스트 메뉴를 추가합니다.
210
- if (isQuizEnabled || isDevMode) {
200
+ if (isQuizEnabled || localStorage.getItem("devMode") === "true") {
211
201
  return [...getQuizContextOptions(editor, theme), ...baseOptions];
212
202
  }
213
203
  return baseOptions;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@team-monolith/cds",
3
- "version": "1.47.0",
3
+ "version": "1.48.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "sideEffects": false,
@@ -1,30 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- */
8
- import type { DOMConversionMap, DOMExportOutput, EditorConfig, LexicalNode, NodeKey, SerializedElementNode, Spread } from "lexical";
9
- import { ElementNode } from "lexical";
10
- export type SerializedLayoutContainerNode = Spread<{
11
- templateColumns: string;
12
- }, SerializedElementNode>;
13
- export declare class LayoutContainerNode extends ElementNode {
14
- __templateColumns: string;
15
- constructor(templateColumns: string, key?: NodeKey);
16
- static getType(): string;
17
- static clone(node: LayoutContainerNode): LayoutContainerNode;
18
- createDOM(config: EditorConfig): HTMLElement;
19
- exportDOM(): DOMExportOutput;
20
- updateDOM(prevNode: LayoutContainerNode, dom: HTMLElement): boolean;
21
- static importDOM(): DOMConversionMap | null;
22
- static importJSON(json: SerializedLayoutContainerNode): LayoutContainerNode;
23
- isShadowRoot(): boolean;
24
- canBeEmpty(): boolean;
25
- exportJSON(): SerializedLayoutContainerNode;
26
- getTemplateColumns(): string;
27
- setTemplateColumns(templateColumns: string): void;
28
- }
29
- export declare function $createLayoutContainerNode(templateColumns: string): LayoutContainerNode;
30
- export declare function $isLayoutContainerNode(node: LexicalNode | null | undefined): node is LayoutContainerNode;
@@ -1,88 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- */
8
- import { addClassNamesToElement } from "@lexical/utils";
9
- import { ElementNode } from "lexical";
10
- function $convertLayoutContainerElement(domNode) {
11
- const styleAttributes = window.getComputedStyle(domNode);
12
- const templateColumns = styleAttributes.getPropertyValue("grid-template-columns");
13
- if (templateColumns) {
14
- const node = $createLayoutContainerNode(templateColumns);
15
- return { node };
16
- }
17
- return null;
18
- }
19
- export class LayoutContainerNode extends ElementNode {
20
- constructor(templateColumns, key) {
21
- super(key);
22
- this.__templateColumns = templateColumns;
23
- }
24
- static getType() {
25
- return "layout-container";
26
- }
27
- static clone(node) {
28
- return new LayoutContainerNode(node.__templateColumns, node.__key);
29
- }
30
- createDOM(config) {
31
- const dom = document.createElement("div");
32
- dom.style.display = "grid";
33
- dom.style.gridTemplateColumns = this.__templateColumns;
34
- if (typeof config.theme.layoutContainer === "string") {
35
- addClassNamesToElement(dom, config.theme.layoutContainer);
36
- }
37
- return dom;
38
- }
39
- exportDOM() {
40
- const element = document.createElement("div");
41
- element.style.gridTemplateColumns = this.__templateColumns;
42
- element.setAttribute("data-lexical-layout-container", "true");
43
- return { element };
44
- }
45
- updateDOM(prevNode, dom) {
46
- if (prevNode.__templateColumns !== this.__templateColumns) {
47
- dom.style.gridTemplateColumns = this.__templateColumns;
48
- }
49
- return false;
50
- }
51
- static importDOM() {
52
- return {
53
- div: (domNode) => {
54
- if (!domNode.hasAttribute("data-lexical-layout-container")) {
55
- return null;
56
- }
57
- return {
58
- conversion: $convertLayoutContainerElement,
59
- priority: 2,
60
- };
61
- },
62
- };
63
- }
64
- static importJSON(json) {
65
- return $createLayoutContainerNode(json.templateColumns);
66
- }
67
- isShadowRoot() {
68
- return true;
69
- }
70
- canBeEmpty() {
71
- return false;
72
- }
73
- exportJSON() {
74
- return Object.assign(Object.assign({}, super.exportJSON()), { templateColumns: this.__templateColumns, type: "layout-container", version: 1 });
75
- }
76
- getTemplateColumns() {
77
- return this.getLatest().__templateColumns;
78
- }
79
- setTemplateColumns(templateColumns) {
80
- this.getWritable().__templateColumns = templateColumns;
81
- }
82
- }
83
- export function $createLayoutContainerNode(templateColumns) {
84
- return new LayoutContainerNode(templateColumns);
85
- }
86
- export function $isLayoutContainerNode(node) {
87
- return node instanceof LayoutContainerNode;
88
- }
@@ -1,22 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- */
8
- import type { DOMConversionMap, EditorConfig, LexicalNode, SerializedElementNode } from "lexical";
9
- import { ElementNode } from "lexical";
10
- export type SerializedLayoutItemNode = SerializedElementNode;
11
- export declare class LayoutItemNode extends ElementNode {
12
- static getType(): string;
13
- static clone(node: LayoutItemNode): LayoutItemNode;
14
- createDOM(config: EditorConfig): HTMLElement;
15
- updateDOM(): boolean;
16
- static importDOM(): DOMConversionMap | null;
17
- static importJSON(): LayoutItemNode;
18
- isShadowRoot(): boolean;
19
- exportJSON(): SerializedLayoutItemNode;
20
- }
21
- export declare function $createLayoutItemNode(): LayoutItemNode;
22
- export declare function $isLayoutItemNode(node: LexicalNode | null | undefined): node is LayoutItemNode;
@@ -1,45 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- */
8
- import { addClassNamesToElement } from "@lexical/utils";
9
- import { ElementNode } from "lexical";
10
- export class LayoutItemNode extends ElementNode {
11
- static getType() {
12
- return "layout-item";
13
- }
14
- static clone(node) {
15
- return new LayoutItemNode(node.__key);
16
- }
17
- createDOM(config) {
18
- const dom = document.createElement("div");
19
- if (typeof config.theme.layoutItem === "string") {
20
- addClassNamesToElement(dom, config.theme.layoutItem);
21
- }
22
- return dom;
23
- }
24
- updateDOM() {
25
- return false;
26
- }
27
- static importDOM() {
28
- return {};
29
- }
30
- static importJSON() {
31
- return $createLayoutItemNode();
32
- }
33
- isShadowRoot() {
34
- return true;
35
- }
36
- exportJSON() {
37
- return Object.assign(Object.assign({}, super.exportJSON()), { type: "layout-item", version: 1 });
38
- }
39
- }
40
- export function $createLayoutItemNode() {
41
- return new LayoutItemNode();
42
- }
43
- export function $isLayoutItemNode(node) {
44
- return node instanceof LayoutItemNode;
45
- }
@@ -1,17 +0,0 @@
1
- /// <reference types="react" />
2
- /**
3
- * Copyright (c) Meta Platforms, Inc. and affiliates.
4
- *
5
- * This source code is licensed under the MIT license found in the
6
- * LICENSE file in the root directory of this source tree.
7
- *
8
- */
9
- import { LexicalEditor } from "lexical";
10
- export declare const LAYOUTS: {
11
- label: string;
12
- value: string;
13
- }[];
14
- export default function InsertLayoutDialog({ activeEditor, onClose, }: {
15
- activeEditor: LexicalEditor;
16
- onClose: () => void;
17
- }): JSX.Element;
@@ -1,21 +0,0 @@
1
- import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
2
- import { useState } from "react";
3
- import { INSERT_LAYOUT_COMMAND } from "./LayoutPlugin";
4
- import Button from "../../../../components/Button";
5
- export const LAYOUTS = [
6
- { label: "2 columns (equal width)", value: "1fr 1fr" },
7
- { label: "2 columns (25% - 75%)", value: "1fr 3fr" },
8
- { label: "3 columns (equal width)", value: "1fr 1fr 1fr" },
9
- { label: "3 columns (25% - 50% - 25%)", value: "1fr 2fr 1fr" },
10
- { label: "4 columns (equal width)", value: "1fr 1fr 1fr 1fr" },
11
- ];
12
- export default function InsertLayoutDialog({ activeEditor, onClose, }) {
13
- var _a;
14
- const [layout, setLayout] = useState(LAYOUTS[0].value);
15
- const buttonLabel = (_a = LAYOUTS.find((item) => item.value === layout)) === null || _a === void 0 ? void 0 : _a.label;
16
- const onClick = () => {
17
- activeEditor.dispatchCommand(INSERT_LAYOUT_COMMAND, LAYOUTS[0].value);
18
- onClose();
19
- };
20
- return (_jsx(_Fragment, { children: _jsx(Button, Object.assign({ onClick: onClick, color: "grey", size: "small", label: undefined }, { children: "Insert" })) }));
21
- }
@@ -1,14 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- */
8
- import type { LexicalCommand, NodeKey } from "lexical";
9
- export declare const INSERT_LAYOUT_COMMAND: LexicalCommand<string>;
10
- export declare const UPDATE_LAYOUT_COMMAND: LexicalCommand<{
11
- template: string;
12
- nodeKey: NodeKey;
13
- }>;
14
- export declare function LayoutPlugin(): null;
@@ -1,125 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- */
8
- import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
9
- import { $findMatchingParent, $insertNodeToNearestRoot, mergeRegister, } from "@lexical/utils";
10
- import { $createParagraphNode, $getNodeByKey, $getSelection, $isRangeSelection, COMMAND_PRIORITY_EDITOR, COMMAND_PRIORITY_LOW, createCommand, KEY_ARROW_DOWN_COMMAND, KEY_ARROW_LEFT_COMMAND, KEY_ARROW_RIGHT_COMMAND, KEY_ARROW_UP_COMMAND, } from "lexical";
11
- import { useEffect } from "react";
12
- import { $createLayoutContainerNode, $isLayoutContainerNode, LayoutContainerNode, } from "../../nodes/LayoutContainerNode";
13
- import { $createLayoutItemNode, $isLayoutItemNode, LayoutItemNode, } from "../../nodes/LayoutItemNode";
14
- export const INSERT_LAYOUT_COMMAND = createCommand();
15
- export const UPDATE_LAYOUT_COMMAND = createCommand();
16
- export function LayoutPlugin() {
17
- const [editor] = useLexicalComposerContext();
18
- useEffect(() => {
19
- if (!editor.hasNodes([LayoutContainerNode, LayoutItemNode])) {
20
- throw new Error("LayoutPlugin: LayoutContainerNode, or LayoutItemNode not registered on editor");
21
- }
22
- const $onEscape = (before) => {
23
- var _a, _b;
24
- const selection = $getSelection();
25
- if ($isRangeSelection(selection) &&
26
- selection.isCollapsed() &&
27
- selection.anchor.offset === 0) {
28
- const container = $findMatchingParent(selection.anchor.getNode(), $isLayoutContainerNode);
29
- if ($isLayoutContainerNode(container)) {
30
- const parent = container.getParent();
31
- const child = parent &&
32
- (before
33
- ? parent.getFirstChild()
34
- : parent === null || parent === void 0 ? void 0 : parent.getLastChild());
35
- const descendant = before
36
- ? (_a = container.getFirstDescendant()) === null || _a === void 0 ? void 0 : _a.getKey()
37
- : (_b = container.getLastDescendant()) === null || _b === void 0 ? void 0 : _b.getKey();
38
- if (parent !== null &&
39
- child === container &&
40
- selection.anchor.key === descendant) {
41
- if (before) {
42
- container.insertBefore($createParagraphNode());
43
- }
44
- else {
45
- container.insertAfter($createParagraphNode());
46
- }
47
- }
48
- }
49
- }
50
- return false;
51
- };
52
- return mergeRegister(
53
- // When layout is the last child pressing down/right arrow will insert paragraph
54
- // below it to allow adding more content. It's similar what $insertBlockNode
55
- // (mainly for decorators), except it'll always be possible to continue adding
56
- // new content even if trailing paragraph is accidentally deleted
57
- editor.registerCommand(KEY_ARROW_DOWN_COMMAND, () => $onEscape(false), COMMAND_PRIORITY_LOW), editor.registerCommand(KEY_ARROW_RIGHT_COMMAND, () => $onEscape(false), COMMAND_PRIORITY_LOW),
58
- // When layout is the first child pressing up/left arrow will insert paragraph
59
- // above it to allow adding more content. It's similar what $insertBlockNode
60
- // (mainly for decorators), except it'll always be possible to continue adding
61
- // new content even if leading paragraph is accidentally deleted
62
- editor.registerCommand(KEY_ARROW_UP_COMMAND, () => $onEscape(true), COMMAND_PRIORITY_LOW), editor.registerCommand(KEY_ARROW_LEFT_COMMAND, () => $onEscape(true), COMMAND_PRIORITY_LOW), editor.registerCommand(INSERT_LAYOUT_COMMAND, (template) => {
63
- editor.update(() => {
64
- const container = $createLayoutContainerNode(template);
65
- const itemsCount = getItemsCountFromTemplate(template);
66
- for (let i = 0; i < itemsCount; i++) {
67
- container.append($createLayoutItemNode().append($createParagraphNode()));
68
- }
69
- $insertNodeToNearestRoot(container);
70
- container.selectStart();
71
- });
72
- return true;
73
- }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(UPDATE_LAYOUT_COMMAND, ({ template, nodeKey }) => {
74
- editor.update(() => {
75
- const container = $getNodeByKey(nodeKey);
76
- if (!$isLayoutContainerNode(container)) {
77
- return;
78
- }
79
- const itemsCount = getItemsCountFromTemplate(template);
80
- const prevItemsCount = getItemsCountFromTemplate(container.getTemplateColumns());
81
- // Add or remove extra columns if new template does not match existing one
82
- if (itemsCount > prevItemsCount) {
83
- for (let i = prevItemsCount; i < itemsCount; i++) {
84
- container.append($createLayoutItemNode().append($createParagraphNode()));
85
- }
86
- }
87
- else if (itemsCount < prevItemsCount) {
88
- for (let i = prevItemsCount - 1; i >= itemsCount; i--) {
89
- const layoutItem = container.getChildAtIndex(i);
90
- if ($isLayoutItemNode(layoutItem)) {
91
- layoutItem.remove();
92
- }
93
- }
94
- }
95
- container.setTemplateColumns(template);
96
- });
97
- return true;
98
- }, COMMAND_PRIORITY_EDITOR),
99
- // Structure enforcing transformers for each node type. In case nesting structure is not
100
- // "Container > Item" it'll unwrap nodes and convert it back
101
- // to regular content.
102
- editor.registerNodeTransform(LayoutItemNode, (node) => {
103
- const parent = node.getParent();
104
- if (!$isLayoutContainerNode(parent)) {
105
- const children = node.getChildren();
106
- for (const child of children) {
107
- node.insertBefore(child);
108
- }
109
- node.remove();
110
- }
111
- }), editor.registerNodeTransform(LayoutContainerNode, (node) => {
112
- const children = node.getChildren();
113
- if (!children.every($isLayoutItemNode)) {
114
- for (const child of children) {
115
- node.insertBefore(child);
116
- }
117
- node.remove();
118
- }
119
- }));
120
- }, [editor]);
121
- return null;
122
- }
123
- function getItemsCountFromTemplate(template) {
124
- return template.trim().split(/\s+/).length;
125
- }