import "./index.scss"; import React = require("react"); import { Workspace } from "@tandem/editor/browser/stores"; import { MetadataKeys } from "@tandem/editor/browser/constants"; import { cssTokenizer } from "@tandem/html-extension/tokenizers"; import { CallbackDispatcher } from "@tandem/mesh"; import { SyntheticSourceLink } from "@tandem/editor/browser/components/common"; import { ApplyFileEditRequest } from "@tandem/sandbox"; import { kebabCase, camelCase } from "lodash"; import { CSSPrettyPaneComponent } from "./pretty"; import { DOMElements, MatchedStyleRule } from "@tandem/html-extension/collections"; import { BaseApplicationComponent, MutationEvent } from "@tandem/common"; import { HashInputComponent, KeyValueInputComponent, IKeyValueInputComponentProps, IKeyValueNameComponentProps } from "@tandem/html-extension/editor/browser/components/common"; import { MatchedCSSStyleRule, SyntheticDOMElement, SyntheticCSSElementStyleRule, getMatchingCSSStyleRules, isInheritedCSSStyleProperty, SyntheticCSSStyle, SyntheticCSSElementStyleRuleMutationTypes, } from "@tandem/synthetic-browser"; export interface ICSSStyleHashInputProps { style: SyntheticCSSStyle; overridden?: any; inherited?: any; renderName?: (props: IKeyValueNameComponentProps) => any; renderValue?: (props: IKeyValueInputComponentProps) => any; setDeclaration: (key, value, oldKey?) => any; } export interface ICSSStylePaneComponentProps extends ICSSStyleHashInputProps { title?: string; titleClassName?: string; pretty?: boolean; inherited?: boolean; renderTitle?(props?: ICSSStylePaneComponentProps): any; } export class CSSStylePropertyComponent extends BaseApplicationComponent { state = { showPrettyInput: false }; togglePrettyInput = () => { this.setState({ showPrettyInput: !this.state.showPrettyInput }); } render() { const props = this.props; return
{this.state.showPrettyInput ? this.renderPrettyInput(props) : null}
} renderPrettyInput(props: IKeyValueInputComponentProps) { return
} } export class CSSStyleHashInputComponent extends React.Component { render() { const { setDeclaration, style, overridden, inherited, renderName } = this.props; const items = []; for (const key of style.getProperties()) { const value = style[key]; items.push({ name: kebabCase(key), value: style[key], overridden: overridden && overridden[key], dim: inherited && !isInheritedCSSStyleProperty(key) } as any); } return } renderItem = (props: IKeyValueInputComponentProps) => { const children = ; return this.props.renderValue ? this.props.renderValue(Object.assign({ children }, props)) : children; } } export class CSSStylePaneComponent extends BaseApplicationComponent { state = { showPrettyInput: false }; render() { const { setDeclaration, renderTitle, title, style, titleClassName, pretty, overridden, inherited } = this.props; const items = []; for (const key of style.getProperties()) { const value = style[key]; items.push({ name: kebabCase(key), value: style[key], overridden: overridden && overridden[key], dim: inherited && !isInheritedCSSStyleProperty(key) }); } return
{ renderTitle ? renderTitle(this.props) : title }
setDeclaration("", "")}>+
{ pretty ? : }
} } // TODO - add some color for the CSS rules class MatchedCSSStyleRuleComponent extends BaseApplicationComponent<{ result: MatchedCSSStyleRule, workspace: Workspace }, any> { private _highlightedElements: SyntheticDOMElement[]; setDeclaration = (name: string, value: string, oldName?: string) => { this.props.result.rule.style.setProperty(name, value, undefined, oldName); const edit = this.props.result.rule.createEdit(); if (value !== "") { edit.setDeclaration(name, value, oldName); } this.props.workspace.applyFileMutations(edit.mutations); } render() { const { result } = this.props; return } onTitleMouseEnter = () => { this._highlightedElements = this.props.workspace.document.querySelectorAll(this.props.result.rule.selector); for (const element of this._highlightedElements) { element.metadata.set(MetadataKeys.HOVERING, true); } } onTitleMouseLeave = () => { for (const element of this._highlightedElements) { element.metadata.set(MetadataKeys.HOVERING, false); } } onTitleClick = () => { // this.props.workspace.select(this._highlightedElements); } renderTitle = () => { return
{ String(this.props.result.rule.selector) }
} } export class ElementCSSPaneComponent extends React.Component<{ workspace: Workspace }, any> { private _rulesWatcher: MatchedCSSRulesCache; componentWillUnmount() { if (this._rulesWatcher) this._rulesWatcher.target = undefined; } render() { if (!this.props.workspace) return null; const { selection } = this.props.workspace; if (!this._rulesWatcher) { this._rulesWatcher = new MatchedCSSRulesCache(); } // CSS Selection pane only works with *one* element. if (selection.length !== 1) return null; const elements = DOMElements.fromArray(selection); if (!elements.length) return null; this._rulesWatcher.target = elements[0]; return
{ this._rulesWatcher.rules.map((matchResult, index) => { return }) }
} } export class MatchedCSSRulesCache { private _target: SyntheticDOMElement; private _matchedRules: MatchedCSSStyleRule[]; private _documentObserver: CallbackDispatcher; constructor() { this._documentObserver = new CallbackDispatcher(this.onDocumentEvent.bind(this)); } set target(value: SyntheticDOMElement){ if (this._target === value) return; if (this._target) { this._target.ownerDocument.unobserve(this._documentObserver); } this._target = value; this.matchRules(); this._target.ownerDocument.observe(this._documentObserver); } get target(): SyntheticDOMElement { return this._target; } get rules(): MatchedCSSStyleRule[] { return this._matchedRules; } private matchRules() { this._matchedRules = getMatchingCSSStyleRules(this._target); } private onDocumentEvent(event: MutationEvent) { if (!event.mutation || event.mutation.type === SyntheticCSSElementStyleRuleMutationTypes.SET_DECLARATION) return; this.matchRules(); } }