|
|
|
@ -1,6 +1,6 @@
|
|
|
|
import Block from '../Block';
|
|
|
|
import Block from '../Block';
|
|
|
|
import Wrapper from './shared/Wrapper';
|
|
|
|
import Wrapper from './shared/Wrapper';
|
|
|
|
import { x } from 'code-red';
|
|
|
|
import { b, x } from 'code-red';
|
|
|
|
import Document from '../../nodes/Document';
|
|
|
|
import Document from '../../nodes/Document';
|
|
|
|
import { Identifier } from 'estree';
|
|
|
|
import { Identifier } from 'estree';
|
|
|
|
import EventHandler from './Element/EventHandler';
|
|
|
|
import EventHandler from './Element/EventHandler';
|
|
|
|
@ -9,6 +9,16 @@ import { TemplateNode } from '../../../interfaces';
|
|
|
|
import Renderer from '../Renderer';
|
|
|
|
import Renderer from '../Renderer';
|
|
|
|
import add_actions from './shared/add_actions';
|
|
|
|
import add_actions from './shared/add_actions';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const associated_events = {
|
|
|
|
|
|
|
|
fullscreenElement: 'fullscreenchange',
|
|
|
|
|
|
|
|
visibilityState: 'visibilitychange'
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const readonly = new Set([
|
|
|
|
|
|
|
|
'fullscreenElement',
|
|
|
|
|
|
|
|
'visibilityState'
|
|
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
export default class DocumentWrapper extends Wrapper {
|
|
|
|
export default class DocumentWrapper extends Wrapper {
|
|
|
|
node: Document;
|
|
|
|
node: Document;
|
|
|
|
handlers: EventHandler[];
|
|
|
|
handlers: EventHandler[];
|
|
|
|
@ -19,7 +29,64 @@ export default class DocumentWrapper extends Wrapper {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
render(block: Block, _parent_node: Identifier, _parent_nodes: Identifier) {
|
|
|
|
render(block: Block, _parent_node: Identifier, _parent_nodes: Identifier) {
|
|
|
|
|
|
|
|
const { renderer } = this;
|
|
|
|
|
|
|
|
const { component } = renderer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const events: Record<string, Array<{ name: string; value: string }>> = {};
|
|
|
|
|
|
|
|
const bindings: Record<string, string> = {};
|
|
|
|
|
|
|
|
|
|
|
|
add_event_handlers(block, x`@_document`, this.handlers);
|
|
|
|
add_event_handlers(block, x`@_document`, this.handlers);
|
|
|
|
add_actions(block, x`@_document`, this.node.actions);
|
|
|
|
add_actions(block, x`@_document`, this.node.actions);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.node.bindings.forEach(binding => {
|
|
|
|
|
|
|
|
// TODO: what if it's a MemberExpression?
|
|
|
|
|
|
|
|
const binding_name = (binding.expression.node as Identifier).name;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// in dev mode, throw if read-only values are written to
|
|
|
|
|
|
|
|
if (readonly.has(binding.name)) {
|
|
|
|
|
|
|
|
renderer.readonly.add(binding_name);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bindings[binding.name] = binding_name;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const associated_event = associated_events[binding.name];
|
|
|
|
|
|
|
|
const property = binding.name;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!events[associated_event]) events[associated_event] = [];
|
|
|
|
|
|
|
|
events[associated_event].push({
|
|
|
|
|
|
|
|
name: binding_name,
|
|
|
|
|
|
|
|
value: property
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Object.keys(events).forEach(event => {
|
|
|
|
|
|
|
|
const id = block.get_unique_name(`ondocument${event}`);
|
|
|
|
|
|
|
|
const props = events[event];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
renderer.add_to_context(id.name);
|
|
|
|
|
|
|
|
const fn = renderer.reference(id.name);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
props.forEach(prop => {
|
|
|
|
|
|
|
|
renderer.meta_bindings.push(
|
|
|
|
|
|
|
|
b`this._state.${prop.name} = @_document.${prop.value};`
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
block.event_listeners.push(x`
|
|
|
|
|
|
|
|
@listen(@_document, "${event}", ${fn})
|
|
|
|
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
component.partly_hoisted.push(b`
|
|
|
|
|
|
|
|
function ${id}() {
|
|
|
|
|
|
|
|
${props.map(prop => renderer.invalidate(prop.name, x`${prop.name} = @_document.${prop.value}`))}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
block.chunks.init.push(b`
|
|
|
|
|
|
|
|
@add_render_callback(${fn});
|
|
|
|
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
component.has_reactive_assignments = true;
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|