mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-13 18:53:59 +02:00
allow setting selection properties on cursor in hyperscript, fixes #2326
This commit is contained in:
@@ -313,6 +313,8 @@ export function createValue(tagName, attributes, children) {
|
|||||||
let selection = children.find(Selection.isSelection)
|
let selection = children.find(Selection.isSelection)
|
||||||
let anchor
|
let anchor
|
||||||
let focus
|
let focus
|
||||||
|
let marks
|
||||||
|
let isFocused
|
||||||
let decorations = []
|
let decorations = []
|
||||||
const partials = {}
|
const partials = {}
|
||||||
|
|
||||||
@@ -320,16 +322,22 @@ export function createValue(tagName, attributes, children) {
|
|||||||
// focus information saved, or decorations applied.
|
// focus information saved, or decorations applied.
|
||||||
if (document) {
|
if (document) {
|
||||||
document.getTexts().forEach(text => {
|
document.getTexts().forEach(text => {
|
||||||
if (text.__anchor != null) {
|
const { __anchor, __decorations, __focus } = text
|
||||||
anchor = Point.create({ key: text.key, offset: text.__anchor.offset })
|
|
||||||
|
if (__anchor != null) {
|
||||||
|
anchor = Point.create({ key: text.key, offset: __anchor.offset })
|
||||||
|
marks = __anchor.marks
|
||||||
|
isFocused = __anchor.isFocused
|
||||||
}
|
}
|
||||||
|
|
||||||
if (text.__focus != null) {
|
if (__focus != null) {
|
||||||
focus = Point.create({ key: text.key, offset: text.__focus.offset })
|
focus = Point.create({ key: text.key, offset: __focus.offset })
|
||||||
|
marks = __focus.marks
|
||||||
|
isFocused = __focus.isFocused
|
||||||
}
|
}
|
||||||
|
|
||||||
if (text.__decorations != null) {
|
if (__decorations != null) {
|
||||||
for (const dec of text.__decorations) {
|
for (const dec of __decorations) {
|
||||||
const { id } = dec
|
const { id } = dec
|
||||||
const partial = partials[id]
|
const partial = partials[id]
|
||||||
delete partials[id]
|
delete partials[id]
|
||||||
@@ -381,7 +389,7 @@ export function createValue(tagName, attributes, children) {
|
|||||||
|
|
||||||
if (anchor || focus) {
|
if (anchor || focus) {
|
||||||
if (!selection) {
|
if (!selection) {
|
||||||
selection = Selection.create({ anchor, focus, isFocused: true })
|
selection = Selection.create({ anchor, focus, isFocused, marks })
|
||||||
} else {
|
} else {
|
||||||
selection = selection.setPoints([anchor, focus])
|
selection = selection.setPoints([anchor, focus])
|
||||||
}
|
}
|
||||||
@@ -414,15 +422,26 @@ export function createValue(tagName, attributes, children) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
class CursorPoint {
|
class CursorPoint {
|
||||||
constructor() {
|
constructor(attrs = {}) {
|
||||||
|
const { isFocused = true, marks = null } = attrs
|
||||||
|
this.isFocused = isFocused
|
||||||
|
this.marks = marks
|
||||||
this.offset = null
|
this.offset = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class AnchorPoint {
|
class AnchorPoint {
|
||||||
constructor(attrs = {}) {
|
constructor(attrs = {}) {
|
||||||
const { key = null, offset = null, path = null } = attrs
|
const {
|
||||||
|
isFocused = true,
|
||||||
|
key = null,
|
||||||
|
marks = null,
|
||||||
|
offset = null,
|
||||||
|
path = null,
|
||||||
|
} = attrs
|
||||||
|
this.isFocused = isFocused
|
||||||
this.key = key
|
this.key = key
|
||||||
|
this.marks = marks
|
||||||
this.offset = offset
|
this.offset = offset
|
||||||
this.path = path
|
this.path = path
|
||||||
}
|
}
|
||||||
@@ -430,8 +449,16 @@ class AnchorPoint {
|
|||||||
|
|
||||||
class FocusPoint {
|
class FocusPoint {
|
||||||
constructor(attrs = {}) {
|
constructor(attrs = {}) {
|
||||||
const { key = null, offset = null, path = null } = attrs
|
const {
|
||||||
|
isFocused = true,
|
||||||
|
key = null,
|
||||||
|
marks = null,
|
||||||
|
offset = null,
|
||||||
|
path = null,
|
||||||
|
} = attrs
|
||||||
|
this.isFocused = isFocused
|
||||||
this.key = key
|
this.key = key
|
||||||
|
this.marks = marks
|
||||||
this.offset = offset
|
this.offset = offset
|
||||||
this.path = path
|
this.path = path
|
||||||
}
|
}
|
||||||
|
65
packages/slate-hyperscript/test/fixtures/cursor-is-focused-false.js
vendored
Normal file
65
packages/slate-hyperscript/test/fixtures/cursor-is-focused-false.js
vendored
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
/** @jsx h */
|
||||||
|
|
||||||
|
import h from 'slate-hyperscript'
|
||||||
|
|
||||||
|
export const input = (
|
||||||
|
<value>
|
||||||
|
<document>
|
||||||
|
<block type="paragraph">
|
||||||
|
<cursor isFocused={false} />one
|
||||||
|
</block>
|
||||||
|
</document>
|
||||||
|
</value>
|
||||||
|
)
|
||||||
|
|
||||||
|
export const options = {
|
||||||
|
preserveSelection: true,
|
||||||
|
preserveKeys: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
export const output = {
|
||||||
|
object: 'value',
|
||||||
|
document: {
|
||||||
|
object: 'document',
|
||||||
|
key: '2',
|
||||||
|
data: {},
|
||||||
|
nodes: [
|
||||||
|
{
|
||||||
|
object: 'block',
|
||||||
|
key: '1',
|
||||||
|
type: 'paragraph',
|
||||||
|
data: {},
|
||||||
|
nodes: [
|
||||||
|
{
|
||||||
|
object: 'text',
|
||||||
|
key: '0',
|
||||||
|
leaves: [
|
||||||
|
{
|
||||||
|
object: 'leaf',
|
||||||
|
text: 'one',
|
||||||
|
marks: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
selection: {
|
||||||
|
object: 'selection',
|
||||||
|
anchor: {
|
||||||
|
object: 'point',
|
||||||
|
key: '0',
|
||||||
|
path: [0, 0],
|
||||||
|
offset: 0,
|
||||||
|
},
|
||||||
|
focus: {
|
||||||
|
object: 'point',
|
||||||
|
key: '0',
|
||||||
|
path: [0, 0],
|
||||||
|
offset: 0,
|
||||||
|
},
|
||||||
|
isFocused: false,
|
||||||
|
marks: null,
|
||||||
|
},
|
||||||
|
}
|
65
packages/slate-hyperscript/test/fixtures/cursor-marks-empty.js
vendored
Normal file
65
packages/slate-hyperscript/test/fixtures/cursor-marks-empty.js
vendored
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
/** @jsx h */
|
||||||
|
|
||||||
|
import h from 'slate-hyperscript'
|
||||||
|
|
||||||
|
export const input = (
|
||||||
|
<value>
|
||||||
|
<document>
|
||||||
|
<block type="paragraph">
|
||||||
|
<cursor marks={[]} />one
|
||||||
|
</block>
|
||||||
|
</document>
|
||||||
|
</value>
|
||||||
|
)
|
||||||
|
|
||||||
|
export const options = {
|
||||||
|
preserveSelection: true,
|
||||||
|
preserveKeys: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
export const output = {
|
||||||
|
object: 'value',
|
||||||
|
document: {
|
||||||
|
object: 'document',
|
||||||
|
key: '2',
|
||||||
|
data: {},
|
||||||
|
nodes: [
|
||||||
|
{
|
||||||
|
object: 'block',
|
||||||
|
key: '1',
|
||||||
|
type: 'paragraph',
|
||||||
|
data: {},
|
||||||
|
nodes: [
|
||||||
|
{
|
||||||
|
object: 'text',
|
||||||
|
key: '0',
|
||||||
|
leaves: [
|
||||||
|
{
|
||||||
|
object: 'leaf',
|
||||||
|
text: 'one',
|
||||||
|
marks: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
selection: {
|
||||||
|
object: 'selection',
|
||||||
|
anchor: {
|
||||||
|
object: 'point',
|
||||||
|
key: '0',
|
||||||
|
path: [0, 0],
|
||||||
|
offset: 0,
|
||||||
|
},
|
||||||
|
focus: {
|
||||||
|
object: 'point',
|
||||||
|
key: '0',
|
||||||
|
path: [0, 0],
|
||||||
|
offset: 0,
|
||||||
|
},
|
||||||
|
isFocused: true,
|
||||||
|
marks: [],
|
||||||
|
},
|
||||||
|
}
|
Reference in New Issue
Block a user