mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-13 10:44:02 +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 anchor
|
||||
let focus
|
||||
let marks
|
||||
let isFocused
|
||||
let decorations = []
|
||||
const partials = {}
|
||||
|
||||
@@ -320,16 +322,22 @@ export function createValue(tagName, attributes, children) {
|
||||
// focus information saved, or decorations applied.
|
||||
if (document) {
|
||||
document.getTexts().forEach(text => {
|
||||
if (text.__anchor != null) {
|
||||
anchor = Point.create({ key: text.key, offset: text.__anchor.offset })
|
||||
const { __anchor, __decorations, __focus } = text
|
||||
|
||||
if (__anchor != null) {
|
||||
anchor = Point.create({ key: text.key, offset: __anchor.offset })
|
||||
marks = __anchor.marks
|
||||
isFocused = __anchor.isFocused
|
||||
}
|
||||
|
||||
if (text.__focus != null) {
|
||||
focus = Point.create({ key: text.key, offset: text.__focus.offset })
|
||||
if (__focus != null) {
|
||||
focus = Point.create({ key: text.key, offset: __focus.offset })
|
||||
marks = __focus.marks
|
||||
isFocused = __focus.isFocused
|
||||
}
|
||||
|
||||
if (text.__decorations != null) {
|
||||
for (const dec of text.__decorations) {
|
||||
if (__decorations != null) {
|
||||
for (const dec of __decorations) {
|
||||
const { id } = dec
|
||||
const partial = partials[id]
|
||||
delete partials[id]
|
||||
@@ -381,7 +389,7 @@ export function createValue(tagName, attributes, children) {
|
||||
|
||||
if (anchor || focus) {
|
||||
if (!selection) {
|
||||
selection = Selection.create({ anchor, focus, isFocused: true })
|
||||
selection = Selection.create({ anchor, focus, isFocused, marks })
|
||||
} else {
|
||||
selection = selection.setPoints([anchor, focus])
|
||||
}
|
||||
@@ -414,15 +422,26 @@ export function createValue(tagName, attributes, children) {
|
||||
*/
|
||||
|
||||
class CursorPoint {
|
||||
constructor() {
|
||||
constructor(attrs = {}) {
|
||||
const { isFocused = true, marks = null } = attrs
|
||||
this.isFocused = isFocused
|
||||
this.marks = marks
|
||||
this.offset = null
|
||||
}
|
||||
}
|
||||
|
||||
class AnchorPoint {
|
||||
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.marks = marks
|
||||
this.offset = offset
|
||||
this.path = path
|
||||
}
|
||||
@@ -430,8 +449,16 @@ class AnchorPoint {
|
||||
|
||||
class FocusPoint {
|
||||
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.marks = marks
|
||||
this.offset = offset
|
||||
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