Skip to content

Commit

Permalink
#720 added script functions for setting and getting cell text in tabl…
Browse files Browse the repository at this point in the history
…e shape (#721)

* #720 added script functions for setting and getting cell text in table shape

* Documented SchemioScript function for table shape
  • Loading branch information
ishubin authored Dec 31, 2024
1 parent 0b8774a commit 93ffabb
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
28 changes: 28 additions & 0 deletions docs/Scripting.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ Schemio offers a possibility of simple scripting. At this moment the Schemio scr
- [ifcond](#ifcond)
- [getEventName](#geteventname)
- [getEventArg](#geteventarg)
- [parseInt](#parseint)
- [parseFloat](#parsefloat)
- [Colors](#colors)
- [Color](#color)
- [decodeColor](#decodecolor)
Expand Down Expand Up @@ -157,6 +159,9 @@ Schemio offers a possibility of simple scripting. At this moment the Schemio scr
- [addBeizerPoint (path)](#addbeizerpoint-path)
- [getPathLength (path)](#getpathlength-path)
- [getPathWorldPosAtLength (path)](#getpathworldposatlength-path)
- [Table functions](#table-functions)
- [setCellText](#setcelltext)
- [getCellText](#getcelltext)
- [Math block functions](#math-block-functions)
- [setExpression](#setexpression)

Expand Down Expand Up @@ -957,6 +962,16 @@ The code above will return `-1` in case if `y` value is `0` and will return `1`
`getEventArg(i)` returns the event argument value at position `i` which is the number of argument starting from 0. It only makes sense to use this function if the event was sent using [sendEvent](#sendevent) function with custom arguments.


#### parseInt

`parseInt(text)` parses an integer from `text` argument and returns it


#### parseFloat

`parseFloat(text)` parses a floating number from `text` argument and returns it


#### Colors

##### Color
Expand Down Expand Up @@ -1576,6 +1591,19 @@ p = pathIrem.getPathWorldPosAtLength(0, 56)
log('x', p.x, 'y', p.y)
```

#### Table functions

`table` shape has special function for interacting with its cells

##### setCellText

`setCellText(row, column, text)` function sets specified text to a cell at the specified row and column

##### getCellText

`getCellText(row, column)` function returns the text of a cell at the specified row and column


#### Math block functions

##### setExpression
Expand Down
49 changes: 48 additions & 1 deletion src/ui/components/editor/items/shapes/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import EditorEventBus from '../../EditorEventBus';
import TablePropertiesEditor from './TablePropertiesEditor.vue';
import utils from '../../../../utils';
import { defaultTextSlotProps } from '../../../../scheme/Item';
import { enrichItemTextSlotWithDefaults } from '../../../../scheme/ItemFixer';

const minCells = 1;
const maxCells = 100;
Expand Down Expand Up @@ -631,6 +632,50 @@ function getPins(item) {
return pins;
}

/**
* @param {String} editorId
* @param {SchemeContainer} schemeContainer
* @param {Item} item
*/
function scriptFunctions(editorId, schemeContainer, item) {
const emitItemChanged = () => {
EditorEventBus.item.changed.specific.$emit(editorId, item.id);
};

return {
setCellText(row, col, text) {
if (row < 0 || row >= item.shapeProps.rows) {
throw new Error(`Cannot set cell text in a table. Row (${row}) is out of bounds [0,${item.shapeProps.rows}]: `);
}
if (col < 0 || col >= item.shapeProps.columns) {
throw new Error(`Cannot set cell text in a table. Cell (${col}) is out of bounds [0,${item.shapeProps.columns}]: `);
}
const key = `c_${row}_${col}`;
if (!item.textSlots[key]) {
item.textSlots[key] = {text: '' + text};
enrichItemTextSlotWithDefaults(item.textSlots[key]);
} else {
item.textSlots[key].text = '' + text;
}
emitItemChanged();
},

getCellText(row, col) {
if (row < 0 || row >= item.shapeProps.rows) {
throw new Error(`Cannot get cell text in a table. Row (${row}) is out of bounds [0,${item.shapeProps.rows}]: `);
}
if (col < 0 || col >= item.shapeProps.columns) {
throw new Error(`Cannot get cell text in a table. Cell (${col}) is out of bounds [0,${item.shapeProps.columns}]: `);
}
const key = `c_${row}_${col}`;
if (item.textSlots[key]) {
return item.textSlots[key].text;
}
return '';
}
};
}

export default {
props: ['item'],
components: {AdvancedFill},
Expand Down Expand Up @@ -880,7 +925,9 @@ export default {
}
return controls;
}
}
},

scriptFunctions,
},

data() {
Expand Down

0 comments on commit 93ffabb

Please sign in to comment.