I'm trying to create a custom code editor as a field within a block. It would eventually become more than just a span.
I'm having a hard time with sizing and whatnot.
Each piece of text is place on a new line (I assume the span feels squished to 0px width and as such the only reason text is shown is due to overflow)
I'd expect this to work with any element, such as an img.
Also, rather than resizing the borderRect, I think it's better to override isFullBlockField, which says to ask the forum before overriding.
Here is what I've got so far. It's filled with a bunch of hacky workarounds.
import * as Blockly from 'blockly/core';
export class CodeField extends Blockly.Field {
constructor(value, validator = null) {
super(value, validator);
this.SERIALIZABLE = true;
}
override initView() {
this.createBorderRect_();
this.textElement_ = Blockly.utils.dom.createSvgElement('foreignObject', {}, this.fieldGroup_);
if (this.getConstants()!.FIELD_TEXT_BASELINE_CENTER) {
this.textElement_.setAttribute('dominant-baseline', 'central');
}
let span = document.createElement('span');
span.innerText = this.value_;
this.textElement_.setAttribute('y', '0');
this.textElement_.appendChild(span);
setTimeout(() => {
this.textElement_.setAttribute('width', String(span.offsetWidth));
this.textElement_.setAttribute('height', String(span.offsetHeight));
this.borderRect_.setAttribute('height', String(span.offsetHeight));
this.size_.height = span.offsetHeight;
}, 0);
}
override positionTextElement_(xOffset: number, contentWidth: number) {
super.positionTextElement_(xOffset, contentWidth);
this.textElement_.setAttribute('y', '0');
}
}
Blockly.fieldRegistry.register('field_code', CodeField);

Thanks all,
Jeremiah