Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP -- nix overlays #143

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/credit-add.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function makeLink (key, label, onClick, theme = 'primary') {
}

function makeClick (key, onClick) {
return function () {
return function (event) {
onClick(key)
}
}
3 changes: 2 additions & 1 deletion src/components/dropdown-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ class DropdownGroup extends React.Component {
)
}
makeOpenMenu (index) {
return () => {
return (event) => {
const {openMenu} = this.state
const toggleOrOpen = (openMenu === index ? null : index)
this.setState({openMenu: toggleOrOpen})
event.stopPropagation()
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/components/editable.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ class Editable extends React.Component {
updatePlaceholderHeights (changes) {
// Do this in a batch, with one widget remeasure/move
for (let i = 0, len = changes.length; i < len; i++) {
const change = changes[i]
const {id, height} = changes[i]
// TODO do this with standard pm.tr interface, not direct DOM
const placeholder = this.refs.mirror.querySelector(`.EdSchemaMedia[grid-id="${change.id}"]`)
placeholder.style.height = change.height + 'px'
const placeholder = this.refs.mirror.querySelector(`.EdSchemaMedia[grid-id="${id}"]`)
placeholder.style.height = height + 'px'
}
this.pm.signal('draw')
}
Expand Down
15 changes: 12 additions & 3 deletions src/components/textarea-autosize.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import {sans} from './rebass-theme'

const containerStyle =
{ fontFamily: sans
, fontSize: '50%'
, fontSize: 12
, marginBottom: '1rem'
}

const areaStyle =
{ fontFamily: 'inherit'
, fontSize: '200%'
, fontSize: 18
, lineHeight: 1.5
, minHeight: '1.5rem'
, display: 'block'
Expand All @@ -30,6 +30,9 @@ function resize () {
textarea.style.height = textarea.scrollHeight + 'px'
}

function stopPropagation (event) {
event.stopPropagation()
}

class TextareaAutosize extends React.Component {
constructor (props) {
Expand All @@ -55,7 +58,13 @@ class TextareaAutosize extends React.Component {
, style: containerStyle
}
, el('label'
, {}
, { onClick: stopPropagation
, onMouseDown: stopPropagation
, onMouseUp: stopPropagation
, onKeyDown: stopPropagation
, onKeyUp: stopPropagation
, onKeyPress: stopPropagation
}
, label
, el('textarea'
, { ref: 'textarea'
Expand Down
3 changes: 3 additions & 0 deletions src/ed.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ function mergeContent (oldContent, newContent) {

// Can't restore selection to a non-focuable (Media) div
function fixSelection (selection, doc) {
if (!selection || !selection.anchor) {
return selection
}
let index = selection.anchor.path[0]
if (doc.content.content[index] && doc.content.content[index].type.contains !== null) {
return selection
Expand Down
44 changes: 13 additions & 31 deletions src/plugins/widget-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,24 @@ export default class WidgetBase {

// Cache these so we don't hit DOM unless needed
this.shown = true
this.top = 0
this.left = 0
this.width = 1
this.height = 50

// Base div container
this.el = document.createElement('div')
this.el.setAttribute('grid-id', options.id)
this.id = options.id
this.initialBlock = options.initialBlock
this.el.style.position = 'absolute'
this.move(options.initialRectangle)
options.widgetContainer.appendChild(this.el)

// Don't let contenteditable flow get events
this.el.addEventListener('mousedown', stopPropagation)
this.el.addEventListener('mouseup', stopPropagation)
this.el.addEventListener('click', stopPropagation)
this.el.addEventListener('keydown', stopPropagation)
this.el.addEventListener('keyup', stopPropagation)
this.el.addEventListener('keypress', stopPropagation)
}
teardown () {
this.el.parentNode.removeChild(this.el)
}
move (rectangle) {
if (this.top !== rectangle.top) {
this.el.style.top = rectangle.top + 'px'
this.top = rectangle.top
}
if (this.left !== rectangle.left) {
this.el.style.left = rectangle.left + 'px'
this.left = rectangle.left
}
if (this.width !== rectangle.width) {
this.el.style.width = rectangle.width + 'px'
this.width = rectangle.width
}
if (this.height !== rectangle.height) {
this.el.style.height = rectangle.height + 'px'
this.height = rectangle.height
}
}
show () {
if (!this.shown) {
this.el.style.display = 'block'
Expand All @@ -53,10 +36,9 @@ export default class WidgetBase {
this.shown = false
}
}
getHeight () {
if (this.el && this.el.firstChild) {
return this.el.firstChild.scrollHeight
}
return this.height
}
}


function stopPropagation (event) {
event.stopPropagation()
}
10 changes: 8 additions & 2 deletions src/plugins/widget-iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,25 @@ export default class WidgetIframe extends WidgetBase {
this.initialBlock = options.initialBlock
this.frame.addEventListener('load', this.postInitialBlock)
}
this.frame.style.width = '100%'
this.frame.src = this.src()
this.el.appendChild(this.frame)

this.height = 100
this.setHeight(100)
}
teardown () {
if (this.initialBlock) {
this.frame.removeEventListener('load', this.postInitialBlock)
}
super.teardown()
}
setHeight (height) {
if (this.height !== height) {
this.frame.style.height = `${height}px`
this.height = height
}
}
getHeight () {
// Don't measure from outside: iframes report own height
return this.height
}
}
48 changes: 9 additions & 39 deletions src/plugins/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,7 @@ function onDOMChanged () {
throw new Error('Bad placeholder!')
}
inDoc.push(id)
const rectangle =
{ top: el.offsetTop
, left: el.offsetLeft
, width: el.offsetWidth
, height: el.offsetHeight
}
this.checkWidget(id, type, rectangle)
this.checkWidget(id, type, el)
}

// Hide or show widgets
Expand All @@ -53,34 +47,14 @@ function onDOMChanged () {
}
}

// Measure inner heights of widgets
let heightChanges = []
for (let i = 0, len = inDOM.length; i < len; i++) {
const id = inDOM[i]
const widget = this.widgets[id]
if (!widget.shown) continue
const innerHeight = widget.getHeight()
if (innerHeight !== widget.height) {
heightChanges.push(
{ id: id
, height: innerHeight
}
)
}
}
if (heightChanges.length) {
// Will trigger a redraw / this onDOMChanged again
this.editableView.updatePlaceholderHeights(heightChanges)
}

// Signal widgets initialized if first
if (!this.initialized) {
this.initialized = true
this.ed.trigger('plugin.widget.initialized')
}
}

function checkWidget (id, type, rectangle) {
function checkWidget (id, type, el) {
let widget = this.widgets[id]
if (widget && widget.type !== type) {
// Remove it
Expand All @@ -90,14 +64,14 @@ function checkWidget (id, type, rectangle) {
}
if (widget) {
// Move it
widget.move(rectangle)
// widget.move(rectangle)
} else {
// Make it
this.initializeWidget(id, type, rectangle)
this.initializeWidget(id, type, el)
}
}

function initializeWidget (id, type, rectangle) {
function initializeWidget (id, type, containerEl) {
let Widget = WidgetTypes[type] || WidgetTypes.react

let initialBlock = this.ed.getBlock(id)
Expand All @@ -107,8 +81,7 @@ function initializeWidget (id, type, rectangle) {
, id
, type
, initialBlock
, widgetContainer: this.el
, initialRectangle: rectangle
, widgetContainer: containerEl
}
)

Expand All @@ -129,12 +102,9 @@ function onIframeMessage (message) {
this.ed.routeChange('MEDIA_BLOCK_UPDATE', block)
break
case 'height':
if (isNaN(message.data.payload)) throw new Error('Iframe height message with non-numeric payload')
this.editableView.updatePlaceholderHeights([
{ id: message.data.id
, height: message.data.payload
}
])
const height = message.data.payload
if (isNaN(height)) throw new Error('Iframe height message with non-numeric payload')
this.widgets[fromId].setHeight(height)
break
case 'cursor':
default:
Expand Down