* Initialize this block using a cross-platform, internationalization-friendly * JSON description. * * @param json Structured data describing the block.
(json: AnyDuringMigration)
| 1753 | * @param json Structured data describing the block. |
| 1754 | */ |
| 1755 | jsonInit(json: AnyDuringMigration) { |
| 1756 | const warningPrefix = json['type'] ? 'Block "' + json['type'] + '": ' : ''; |
| 1757 | |
| 1758 | // Validate inputs. |
| 1759 | if (json['output'] && json['previousStatement']) { |
| 1760 | throw Error( |
| 1761 | warningPrefix + 'Must not have both an output and a previousStatement.', |
| 1762 | ); |
| 1763 | } |
| 1764 | |
| 1765 | // Validate that each arg has a corresponding message |
| 1766 | let n = 0; |
| 1767 | while (json[`args${n}`]) { |
| 1768 | if (json[`message${n}`] === undefined) { |
| 1769 | throw Error( |
| 1770 | warningPrefix + |
| 1771 | `args${n} must have a corresponding message (message${n}).`, |
| 1772 | ); |
| 1773 | } |
| 1774 | n++; |
| 1775 | } |
| 1776 | |
| 1777 | // Set basic properties of block. |
| 1778 | // Handle legacy style object format for backwards compatibility |
| 1779 | if (json['style'] && typeof json['style'] === 'object') { |
| 1780 | this.hat = (json['style'] as {hat?: string}).hat; |
| 1781 | // Must set to null so it doesn't error when checking for style and |
| 1782 | // colour. |
| 1783 | json['style'] = null; |
| 1784 | } |
| 1785 | if (json['style'] && json['colour']) { |
| 1786 | throw Error(warningPrefix + 'Must not have both a colour and a style.'); |
| 1787 | } else if (json['style']) { |
| 1788 | this.jsonInitStyle(json, warningPrefix); |
| 1789 | } else { |
| 1790 | this.jsonInitColour(json, warningPrefix); |
| 1791 | } |
| 1792 | |
| 1793 | // Interpolate the message blocks. |
| 1794 | let i = 0; |
| 1795 | while (json[`message${i}`] !== undefined) { |
| 1796 | this.interpolate( |
| 1797 | json[`message${i}`] || '', |
| 1798 | json[`args${i}`] || [], |
| 1799 | // Backwards compatibility: lastDummyAlign aliases implicitAlign. |
| 1800 | json[`implicitAlign${i}`] || (json as any)[`lastDummyAlign${i}`], |
| 1801 | warningPrefix, |
| 1802 | ); |
| 1803 | i++; |
| 1804 | } |
| 1805 | |
| 1806 | if (json['inputsInline'] !== undefined) { |
| 1807 | eventUtils.disable(); |
| 1808 | this.setInputsInline(json['inputsInline']); |
| 1809 | eventUtils.enable(); |
| 1810 | } |
| 1811 | |
| 1812 | // Set output and previous/next connections. |
no test coverage detected