Skip to content
Snippets Groups Projects
Commit 014c4fbf authored by Tim's avatar Tim
Browse files

insert image as inline, add shortcuts #185

parent d85f3b89
No related branches found
No related tags found
No related merge requests found
...@@ -28,7 +28,7 @@ if(process.env.BROWSER) { ...@@ -28,7 +28,7 @@ if(process.env.BROWSER) {
InsertImages({ InsertImages({
extensions: ['jpeg'], extensions: ['jpeg'],
applyTransform: (transform, file) => { applyTransform: (transform, file) => {
return transform.insertBlock({ return transform.insertInline({
type: 'image', type: 'image',
isVoid: true, isVoid: true,
data: { file } data: { file }
...@@ -160,7 +160,7 @@ export default class SlateEditor extends React.Component { ...@@ -160,7 +160,7 @@ export default class SlateEditor extends React.Component {
const hasLinks = this.hasInline('link') const hasLinks = this.hasInline('link')
if (hasLinks) { if (hasLinks) {
console.log(JSON.stringify(Raw.serialize(state, {terse: false}))) console.log(JSON.stringify(Raw.serialize(state, {terse: false}), null, 2))
state = state state = state
.transform() .transform()
.unwrapInline('link') .unwrapInline('link')
...@@ -195,13 +195,14 @@ console.log(JSON.stringify(Raw.serialize(state, {terse: false}))) ...@@ -195,13 +195,14 @@ console.log(JSON.stringify(Raw.serialize(state, {terse: false})))
.collapseToEnd() .collapseToEnd()
.apply() .apply()
} }
console.log(JSON.stringify(Raw.serialize(state, {terse: false}))) console.log(JSON.stringify(Raw.serialize(state, {terse: false}), null, 2))
this.setState({ state }) this.setState({ state })
} }
// Markdown-style quick formatting // Markdown-style quick formatting
onKeyDown = (e, data, state) => { onKeyDown = (e, data, state) => {
if(data.isMod) return this.onModKeyDown(e, data, state);
switch (data.key) { switch (data.key) {
case 'space': return this.onSpace(e, state) case 'space': return this.onSpace(e, state)
case 'backspace': return this.onBackspace(e, state) case 'backspace': return this.onBackspace(e, state)
...@@ -209,6 +210,33 @@ console.log(JSON.stringify(Raw.serialize(state, {terse: false}))) ...@@ -209,6 +210,33 @@ console.log(JSON.stringify(Raw.serialize(state, {terse: false})))
} }
} }
onModKeyDown = (e, data, state) => {
let mark
switch (data.key) {
case 'b':
mark = 'bold'
break
case 'i':
mark = 'italic'
break
case 'u':
mark = 'underline'
break
case 'k':
return this.onClickLink(e);
}
if(!mark) return;
state = state
.transform()
.toggleMark(mark)
.apply()
e.preventDefault()
return state
}
// If space was entered, check if it was a markdown sequence // If space was entered, check if it was a markdown sequence
onSpace = (e, state) => { onSpace = (e, state) => {
if (state.isExpanded) return if (state.isExpanded) return
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
a { a {
border-bottom: 1px dotted #00f; border-bottom: 1px dotted #00f;
position: relative; position: relative;
img {border: 1px dotted #00f}
} }
.active { .active {
box-shadow: 0 0 0 2px blue; box-shadow: 0 0 0 2px blue;
......
...@@ -51,11 +51,16 @@ export const HtmlRules = [ ...@@ -51,11 +51,16 @@ export const HtmlRules = [
deserialize: (el, next) => { deserialize: (el, next) => {
const type = BLOCK_TAGS[el.tagName] const type = BLOCK_TAGS[el.tagName]
if (!type) return if (!type) return
// Special case for <pre>: ignore its inner <code> element.
const code = el.tagName == 'pre' ? el.children[0] : null
const children = code && code.tagName == 'code' ? code.children : el.children
return { return {
kind: 'block', kind: 'block',
type: type, type: type,
isVoid: (type == 'hr'), isVoid: (type == 'hr'),
nodes: next(el.children) nodes: next(children)
} }
}, },
serialize: (object, children) => { serialize: (object, children) => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment