]> BookStack Code Mirror - bookstack/commitdiff
Lexical: Linked row properties form up
authorDan Brown <redacted>
Fri, 9 Aug 2024 11:42:04 +0000 (12:42 +0100)
committerDan Brown <redacted>
Fri, 9 Aug 2024 11:42:04 +0000 (12:42 +0100)
resources/js/wysiwyg/todo.md
resources/js/wysiwyg/ui/defaults/buttons/tables.ts
resources/js/wysiwyg/ui/defaults/forms/tables.ts
resources/js/wysiwyg/utils/tables.ts

index 2ca9b97dcb7562d1426145b16621c6531238c69b..cf24ad677f63df42c9283d8e1115ec09902f0fa5 100644 (file)
@@ -3,7 +3,6 @@
 ## In progress
 
 - Table features
-  - Row properties form logic
   - Table properties form logic
     - Caption text support 
   - Resize to contents button
index 88ea56186ad39f396e5705af935eed722489cb0c..50353961f0ff47933a6e3d0da3ec7dad0851209e 100644 (file)
@@ -20,8 +20,9 @@ import {
 import {$getNodeFromSelection, $selectionContainsNodeType} from "../../../utils/selection";
 import {$getParentOfType} from "../../../utils/nodes";
 import {$isCustomTableCellNode} from "../../../nodes/custom-table-cell";
-import {$showCellPropertiesForm} from "../forms/tables";
+import {$showCellPropertiesForm, $showRowPropertiesForm} from "../forms/tables";
 import {$mergeTableCellsInSelection} from "../../../utils/tables";
+import {$isCustomTableRowNode} from "../../../nodes/custom-table-row";
 
 const neverActive = (): boolean => false;
 const cellNotSelected = (selection: BaseSelection|null) => !$selectionContainsNodeType(selection, $isCustomTableCellNode);
@@ -166,10 +167,10 @@ export const rowProperties: EditorButtonDefinition = {
                 return;
             }
 
-            const row = $getParentOfType(cell, $isTableRowNode);
-            const modalForm = context.manager.createModal('row_properties');
-            modalForm.show({});
-            // TODO
+            const row = $getParentOfType(cell, $isCustomTableRowNode);
+            if ($isCustomTableRowNode(row)) {
+                $showRowPropertiesForm(row, context);
+            }
         });
     },
     isActive: neverActive,
index 1c577b72a5094c23aef718d6cb62b40aabe3806b..c4879efae66163bd0798fedd8d60e43450b26f88 100644 (file)
@@ -8,8 +8,14 @@ import {EditorUiContext} from "../../framework/core";
 import {CustomTableCellNode} from "../../../nodes/custom-table-cell";
 import {EditorFormModal} from "../../framework/modals";
 import {$getSelection, ElementFormatType} from "lexical";
-import {$getTableCellColumnWidth, $getTableCellsFromSelection, $setTableCellColumnWidth} from "../../../utils/tables";
+import {
+    $getTableCellColumnWidth,
+    $getTableCellsFromSelection,
+    $getTableRowsFromSelection,
+    $setTableCellColumnWidth
+} from "../../../utils/tables";
 import {formatSizeValue} from "../../../utils/dom";
+import {CustomTableRowNode} from "../../../nodes/custom-table-row";
 
 const borderStyleInput: EditorSelectFormFieldDefinition = {
     label: 'Border style',
@@ -164,10 +170,32 @@ export const cellProperties: EditorFormDefinition = {
     ],
 };
 
+export function $showRowPropertiesForm(row: CustomTableRowNode, context: EditorUiContext): EditorFormModal {
+    const styles = row.getStyles();
+    const modalForm = context.manager.createModal('row_properties');
+    modalForm.show({
+        height: styles.get('height') || '',
+        border_style: styles.get('border-style') || '',
+        border_color: styles.get('border-color') || '',
+        background_color: styles.get('background-color') || '',
+    });
+    return modalForm;
+}
+
 export const rowProperties: EditorFormDefinition = {
     submitText: 'Save',
     async action(formData, context: EditorUiContext) {
-        // TODO
+        context.editor.update(() => {
+            const rows = $getTableRowsFromSelection($getSelection());
+            for (const row of rows) {
+                const styles = row.getStyles();
+                styles.set('height', formatSizeValue(formData.get('height')?.toString() || ''));
+                styles.set('border-style', formData.get('border_style')?.toString() || '');
+                styles.set('border-color', formData.get('border_color')?.toString() || '');
+                styles.set('background-color', formData.get('background_color')?.toString() || '');
+                row.setStyles(styles);
+            }
+        });
         return true;
     },
     fields: [
index d92f56c8205daee69e884fa7e75cbae0d174cd9e..e808fd595cccd84e9283aba95456041895b0596d 100644 (file)
@@ -6,6 +6,7 @@ import {$getParentOfType} from "./nodes";
 import {$getNodeFromSelection} from "./selection";
 import {formatSizeValue} from "./dom";
 import {TableMap} from "./table-map";
+import {$isCustomTableRowNode, CustomTableRowNode} from "../nodes/custom-table-row";
 
 function $getTableFromCell(cell: CustomTableCellNode): CustomTableNode|null {
     return $getParentOfType(cell, $isCustomTableNode) as CustomTableNode|null;
@@ -192,6 +193,19 @@ export function $mergeTableCellsInSelection(selection: TableSelection): void {
     firstCell.setRowSpan(newHeight);
 }
 
+export function $getTableRowsFromSelection(selection: BaseSelection|null): CustomTableRowNode[] {
+    const cells = $getTableCellsFromSelection(selection);
+    const rowsByKey: Record<string, CustomTableRowNode> = {};
+    for (const cell of cells) {
+        const row = cell.getParent();
+        if ($isCustomTableRowNode(row)) {
+            rowsByKey[row.getKey()] = row;
+        }
+    }
+
+    return Object.values(rowsByKey);
+}
+