Finding that code generated by Copy As > JavaScript doesn’t yet manage to rebuild a table which includes gaps, I tried to reconstruct such a table manually through omniJS:
This led to one reproducible crash, and one apparent bug (Graphic.remove()
fails on graphics within tables).
Reproducing the crash
In the GUI, we can delete a tile from a table without losing the table’s ability to extend and automatically add cells in either axis.
Table.graphicAt(row, col)
returns a null if we reference the coordinate of a gap, but if we try to construct a table that includes a gap through omniJS, using an array that includes a null, or an empty array, we can accidentally bring the house down:
Don’t try this in the Console, for example - it crashes this build: 7.4 (v179.8 r291433)
Table.withRowsColumns(1, 1, [])
Reproducing the bug
( Graphic.remove()
fails without error on graphics inside tables )
My second approach to rebuilding the table above with omniJS was to use Graphic.remove()
to emulate manual deletion of one or more cells in the GUI.
The code runs fine, and throws no errors, but the graphic is not deleted.
If we first .ungroup()
a table, .remove()
then works, but our structure has now lost the status and powers of a Table.
Test code below
Paste the following function into the console, and invoke it by entering:
omniJSONContext()
followed by the return key.
// omniJSONContext :: OG () -> JSON String
const omniJSONContext = () => {
// GENERIC FUNCTIONS -------------------------------------------------
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = (m, n) =>
Array.from({
length: Math.floor(n - m) + 1
}, (_, i) => m + i);
// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) => xs.map(f);
// show :: Int -> a -> Indented String
// show :: a -> String
const show = (...x) =>
JSON.stringify.apply(
null, x.length > 1 ? [x[1], null, x[0]] : x
);
const
dctStyle = {
cornerRadius: 5,
fillColor: Color.RGB(1, 0.75, 0.75),
geometry: new Rect(
104.72391558679072, -188.10624072930537,
131.8110248179886,
104.88189071538886
),
text: '-', // if the text is zero length, formatting is lost
//fontName: "Monaco",
autosizing: TextAutosizing.Clip,
textHorizontalAlignment: HorizontalTextAlignment.Center,
textSize: 15,
textVerticalPadding: 0
},
cnv = document.windows[0].selection.canvas,
tbl = Table.withRowsColumns(
3, 3,
map(
n => Object.assign(
cnv.newShape(),
dctStyle, {
text: (n)
.toString()
}
),
enumFromTo(1, 9)
)
),
oMiddleTile = tbl.graphicAt(1, 1);
// tbl.ungroup(); // Remove will work if we ungroup, but then,
// the structure loses the powers and properties of a table.
oMiddleTile.remove();
const strJSON = show({
message: 'Graphic ' + oMiddleTile.id + ' has not been removed ...'
});
// Save a return value for JXA to find in Canvas background user-data
return (
cnv.background.setUserData('omniJSON', strJSON),
strJSON
);
};