| 101 | } |
| 102 | |
| 103 | func (c *DocsSedCmd) runTableCreate(ctx context.Context, u *ui.UI, account, id string, expr sedExpr, spec *tableCreateSpec) error { |
| 104 | planner, err := docssed.NewTableCreatePlanner( |
| 105 | semanticExpressionFromSedExpr(expr), |
| 106 | docssed.TableCreateSpec{ |
| 107 | Rows: spec.rows, |
| 108 | Columns: spec.cols, |
| 109 | Header: spec.header, |
| 110 | Cells: spec.cells, |
| 111 | }, |
| 112 | ) |
| 113 | if err != nil { |
| 114 | return err |
| 115 | } |
| 116 | |
| 117 | docsSvc, doc, err := fetchDoc(ctx, account, id) |
| 118 | if err != nil { |
| 119 | return err |
| 120 | } |
| 121 | |
| 122 | projection := docssed.ProjectDocument(doc) |
| 123 | var mutation *docssed.TableCreateMutation |
| 124 | if projection.Legacy != nil { |
| 125 | mutation = planner.Plan(*projection.Legacy) |
| 126 | } |
| 127 | if mutation == nil { |
| 128 | return sedOutputOK(ctx, u, id, sedOutputKV{"replaced", 0}, sedOutputKV{"message", "pattern not found"}) |
| 129 | } |
| 130 | |
| 131 | // Step 1: Delete the placeholder text |
| 132 | // Step 2: Insert the table at that position |
| 133 | // Note: InsertTableRequest requires the location to be inside a paragraph, |
| 134 | // so we insert at the start of the match. |
| 135 | var requests []*docs.Request |
| 136 | |
| 137 | // Delete placeholder text |
| 138 | if mutation.StartIndex < mutation.EndIndex { |
| 139 | requests = append(requests, &docs.Request{ |
| 140 | DeleteContentRange: &docs.DeleteContentRangeRequest{ |
| 141 | Range: &docs.Range{ |
| 142 | StartIndex: mutation.StartIndex, |
| 143 | EndIndex: mutation.EndIndex, |
| 144 | }, |
| 145 | }, |
| 146 | }) |
| 147 | } |
| 148 | |
| 149 | // Insert table at the position where placeholder was |
| 150 | requests = append(requests, &docs.Request{ |
| 151 | InsertTable: &docs.InsertTableRequest{ |
| 152 | Location: &docs.Location{Index: mutation.StartIndex}, |
| 153 | Rows: int64(mutation.Rows), |
| 154 | Columns: int64(mutation.Columns), |
| 155 | }, |
| 156 | }) |
| 157 | |
| 158 | _, err = batchUpdate(ctx, docsSvc, id, requests) |
| 159 | if err != nil { |
| 160 | return fmt.Errorf("batch update (create table): %w", err) |