code
stringlengths 24
2.07M
| docstring
stringlengths 25
85.3k
| func_name
stringlengths 1
92
| language
stringclasses 1
value | repo
stringlengths 5
64
| path
stringlengths 4
172
| url
stringlengths 44
218
| license
stringclasses 7
values |
---|---|---|---|---|---|---|---|
reduceOt = function(currentAdvance, char) {
return currentAdvance + calcCharAdvanceOpenType(char, fontSize, font, unitsPerEm)
}
|
Tests various string widths and compares the advance width (in pixels) results between OpenType.js and
the canvas fallback mechanism which returns the browser's actual rendered font width.
@type {Function}
|
reduceOt
|
javascript
|
ritzyed/ritzy
|
src/core/TextFontMetrics.js
|
https://github.com/ritzyed/ritzy/blob/master/src/core/TextFontMetrics.js
|
Apache-2.0
|
reduceCanvas = function(currentAdvance, char) {
return currentAdvance + calcTextAdvanceCanvas(char, fontSize, font)
}
|
Tests various string widths and compares the advance width (in pixels) results between OpenType.js and
the canvas fallback mechanism which returns the browser's actual rendered font width.
@type {Function}
|
reduceCanvas
|
javascript
|
ritzyed/ritzy
|
src/core/TextFontMetrics.js
|
https://github.com/ritzyed/ritzy/blob/master/src/core/TextFontMetrics.js
|
Apache-2.0
|
reduceCanvas = function(currentAdvance, char) {
return currentAdvance + calcTextAdvanceCanvas(char, fontSize, font)
}
|
Tests various string widths and compares the advance width (in pixels) results between OpenType.js and
the canvas fallback mechanism which returns the browser's actual rendered font width.
@type {Function}
|
reduceCanvas
|
javascript
|
ritzyed/ritzy
|
src/core/TextFontMetrics.js
|
https://github.com/ritzyed/ritzy/blob/master/src/core/TextFontMetrics.js
|
Apache-2.0
|
function calcCharAdvance(char, fontSize, font, unitsPerEm) {
return isOpenTypeJsReliable(fontSize, font, unitsPerEm) ?
calcCharAdvanceOpenType(char, fontSize, font, unitsPerEm) :
calcTextAdvanceCanvas(char, fontSize, font)
}
|
Calculate the advance in pixels for a given char. In some browsers/platforms/font sizes, the fonts are not
rendered according to the specs in the font (see
http://stackoverflow.com/questions/30922573/firefox-rendering-of-opentype-font-does-not-match-the-font-specification).
Therefore, ensure the font spec matches the actual rendered width (via the canvas `measureText` method), and use
the font spec if it matches, otherwise fall back to the (slower) measuredText option.
NOTE there may still be one difference between the browser's rendering and canvas-based calculations here: the
browser renders entire strings within elements, whereas this calculation renders one character to the canvas at
a time and adds up the widths. These two approaches seem to be equivalent except for IE in compatibility mode.
TODO refactor mixin to deal with chunks of styled text rather than chars for IE in compatibility mode
|
calcCharAdvance
|
javascript
|
ritzyed/ritzy
|
src/core/TextFontMetrics.js
|
https://github.com/ritzyed/ritzy/blob/master/src/core/TextFontMetrics.js
|
Apache-2.0
|
function calcReplicaCharAdvance(replicaChar, fontSize, fonts, minFontSize, unitsPerEm) {
let style = charFontStyle(replicaChar)
let charFontSize = calcFontSizeFromAttributes(fontSize, minFontSize, replicaChar.attributes)
return calcCharAdvance(replicaChar.char, charFontSize, fonts[style], unitsPerEm)
}
|
Calculate the advance in pixels for a given char. In some browsers/platforms/font sizes, the fonts are not
rendered according to the specs in the font (see
http://stackoverflow.com/questions/30922573/firefox-rendering-of-opentype-font-does-not-match-the-font-specification).
Therefore, ensure the font spec matches the actual rendered width (via the canvas `measureText` method), and use
the font spec if it matches, otherwise fall back to the (slower) measuredText option.
NOTE there may still be one difference between the browser's rendering and canvas-based calculations here: the
browser renders entire strings within elements, whereas this calculation renders one character to the canvas at
a time and adds up the widths. These two approaches seem to be equivalent except for IE in compatibility mode.
TODO refactor mixin to deal with chunks of styled text rather than chars for IE in compatibility mode
|
calcReplicaCharAdvance
|
javascript
|
ritzyed/ritzy
|
src/core/TextFontMetrics.js
|
https://github.com/ritzyed/ritzy/blob/master/src/core/TextFontMetrics.js
|
Apache-2.0
|
setConfig(config) {
this.config = config
}
|
Calculate the advance in pixels for a given char. In some browsers/platforms/font sizes, the fonts are not
rendered according to the specs in the font (see
http://stackoverflow.com/questions/30922573/firefox-rendering-of-opentype-font-does-not-match-the-font-specification).
Therefore, ensure the font spec matches the actual rendered width (via the canvas `measureText` method), and use
the font spec if it matches, otherwise fall back to the (slower) measuredText option.
NOTE there may still be one difference between the browser's rendering and canvas-based calculations here: the
browser renders entire strings within elements, whereas this calculation renders one character to the canvas at
a time and adds up the widths. These two approaches seem to be equivalent except for IE in compatibility mode.
TODO refactor mixin to deal with chunks of styled text rather than chars for IE in compatibility mode
|
setConfig
|
javascript
|
ritzyed/ritzy
|
src/core/TextFontMetrics.js
|
https://github.com/ritzyed/ritzy/blob/master/src/core/TextFontMetrics.js
|
Apache-2.0
|
fontScale(fontSize) {
return calcFontScale(fontSize, this.config.unitsPerEm)
}
|
Get the font scale to convert between font units and pixels for the given font size.
@param fontSize
@return {number}
|
fontScale
|
javascript
|
ritzyed/ritzy
|
src/core/TextFontMetrics.js
|
https://github.com/ritzyed/ritzy/blob/master/src/core/TextFontMetrics.js
|
Apache-2.0
|
fontSizeFromAttributes(fontSize, attributes) {
return calcFontSizeFromAttributes(fontSize, this.config.minFontSize, attributes)
}
|
Return the font size given the default font size and current attributes.
@param fontSize
@param attributes
|
fontSizeFromAttributes
|
javascript
|
ritzyed/ritzy
|
src/core/TextFontMetrics.js
|
https://github.com/ritzyed/ritzy/blob/master/src/core/TextFontMetrics.js
|
Apache-2.0
|
replicaCharAdvance(char, fontSize) {
return calcReplicaCharAdvance(char, fontSize, this.config.fonts, this.config.minFontSize, this.config.unitsPerEm)
}
|
Determines the advance for a given replica char.
@param char The replica char object.
@param fontSize
@return {number}
|
replicaCharAdvance
|
javascript
|
ritzyed/ritzy
|
src/core/TextFontMetrics.js
|
https://github.com/ritzyed/ritzy/blob/master/src/core/TextFontMetrics.js
|
Apache-2.0
|
charAdvance(char, fontSize, font) {
return calcCharAdvance(char, fontSize, font, this.config.unitsPerEm)
}
|
Determines the advance for a given char. Since it is not a replica char, the font style and other attribute
information cannot be determined. A normal weight, non-decorated font with no special attributes is assumed.
|
charAdvance
|
javascript
|
ritzyed/ritzy
|
src/core/TextFontMetrics.js
|
https://github.com/ritzyed/ritzy/blob/master/src/core/TextFontMetrics.js
|
Apache-2.0
|
advanceXForSpace(fontSize) {
return this.charAdvance(' ', fontSize, this.config.fonts.regular)
}
|
Returns the advance width in pixels for a space character in the normal style.
|
advanceXForSpace
|
javascript
|
ritzyed/ritzy
|
src/core/TextFontMetrics.js
|
https://github.com/ritzyed/ritzy/blob/master/src/core/TextFontMetrics.js
|
Apache-2.0
|
indexAndCursorForXValue(fontSize, pixelValue, chars) {
let minFontSize = this.config.minFontSize
fontSize = fontSize > minFontSize ? fontSize : minFontSize
let currentWidthPx = 0
let index = 0
for(let i = 0; i < chars.length; i++) {
let glyphAdvancePx = this.replicaCharAdvance(chars[i], fontSize)
if(pixelValue < currentWidthPx + glyphAdvancePx / 2) {
return {
cursorX: currentWidthPx,
index: index
}
} else {
currentWidthPx += glyphAdvancePx
if(glyphAdvancePx > 0) index++
}
}
return {
cursorX: currentWidthPx,
index: index
}
}
|
Obtain an Object with the char id and cursor position for a given pixel value. This is used to
set the current character and position the cursor correctly on a mouse click. If the target position
is past the last character, the index of the last character is returned.
@param {number} fontSize
@param {number} pixelValue
@param {Array} chars The characters used to compare against the given pixel value.
@return {Object} The cursor x position (cursorX) between characters, and the 0-based character index
(index) for the given x value.
|
indexAndCursorForXValue
|
javascript
|
ritzyed/ritzy
|
src/core/TextFontMetrics.js
|
https://github.com/ritzyed/ritzy/blob/master/src/core/TextFontMetrics.js
|
Apache-2.0
|
advanceXForChars(fontSize, chars) {
let minFontSize = this.config.minFontSize
fontSize = fontSize > minFontSize ? fontSize : minFontSize
if(_.isArray(chars)) {
return chars.reduce((currentWidthPx, char) => {
return currentWidthPx + this.replicaCharAdvance(char, fontSize)
}, 0)
} else {
return this.replicaCharAdvance(chars, fontSize)
}
}
|
Get the advance width in pixels for the given char or chars.
@param {number} fontSize
@param {object|Array} chars
@return {number}
|
advanceXForChars
|
javascript
|
ritzyed/ritzy
|
src/core/TextFontMetrics.js
|
https://github.com/ritzyed/ritzy/blob/master/src/core/TextFontMetrics.js
|
Apache-2.0
|
lineHeight(fontSize) {
let fontHeader = this.config.fonts.bold.tables.head
return (fontHeader.yMax - fontHeader.yMin) * this.fontScale(fontSize)
}
|
Gets the line height in pixels for a given font size, using the bold font.
|
lineHeight
|
javascript
|
ritzyed/ritzy
|
src/core/TextFontMetrics.js
|
https://github.com/ritzyed/ritzy/blob/master/src/core/TextFontMetrics.js
|
Apache-2.0
|
top(fontSize) {
let fontHeader = this.config.fonts.bold.tables.head
return fontHeader.yMax * this.fontScale(fontSize)
}
|
Gets the height in pixels of the top of the font, relative to the baseline, using the bold font.
|
top
|
javascript
|
ritzyed/ritzy
|
src/core/TextFontMetrics.js
|
https://github.com/ritzyed/ritzy/blob/master/src/core/TextFontMetrics.js
|
Apache-2.0
|
function writeText(chunks) {
let text = ''
if(!chunks) {
chunks = []
}
chunks.forEach(c => {
text += c.text
})
return text
}
|
Writes chunks of rich text into a plain text. The implementation is simple: just strip any style
information from the rich text.
@param {Array} chunks The rich text chunks to write.
|
writeText
|
javascript
|
ritzyed/ritzy
|
src/core/textwriter.js
|
https://github.com/ritzyed/ritzy/blob/master/src/core/textwriter.js
|
Apache-2.0
|
function isWhitespace(char) {
return WHITESPACE_REGEX.test(char)
}
|
Based on https://github.com/timdown/rangy/blob/master/src/modules/rangy-textrange.js#L119
|
isWhitespace
|
javascript
|
ritzyed/ritzy
|
src/core/tokenizer.js
|
https://github.com/ritzyed/ritzy/blob/master/src/core/tokenizer.js
|
Apache-2.0
|
function tokenizer(chars, wordOptions) {
let word = _.isArray(chars) ? chars.join('') : chars
let result
let tokenRanges = []
// by default if our options include leading spaces but trailing is not specified, turn off trailing
if(wordOptions && wordOptions.includeLeadingSpace && !wordOptions.includeTrailingSpace) {
wordOptions.includeTrailingSpace = false
}
wordOptions = _.merge({}, DEFAULT_WORD_OPTIONS.en, wordOptions)
let createTokenRange = function(start, end, isWord) {
tokenRanges.push({start: start, end: end, isWord: isWord})
}
// Match words and mark characters
let lastWordEnd = 0
let wordStart
let wordEnd
while ((result = wordOptions.wordRegex.exec(word))) {
wordStart = result.index
wordEnd = wordStart + result[0].length
// Get leading space characters for word
if (wordOptions.includeLeadingSpace) {
while (isWhitespace(chars[wordStart - 1])) {
--wordStart
}
}
// Create token for non-word characters preceding this word
if (wordStart > lastWordEnd) {
createTokenRange(lastWordEnd, wordStart, false)
}
// Get trailing space characters for word
if (wordOptions.includeTrailingSpace) {
while (isWhitespace(chars[wordEnd])) {
++wordEnd
}
}
createTokenRange(wordStart, wordEnd, true)
lastWordEnd = wordEnd
}
// Create token for trailing non-word characters, if any exist
if (lastWordEnd < chars.length) {
createTokenRange(lastWordEnd, chars.length, false)
}
return tokenRanges
}
|
Based on https://github.com/timdown/rangy/blob/master/src/modules/rangy-textrange.js#L119
|
tokenizer
|
javascript
|
ritzyed/ritzy
|
src/core/tokenizer.js
|
https://github.com/ritzyed/ritzy/blob/master/src/core/tokenizer.js
|
Apache-2.0
|
createTokenRange = function(start, end, isWord) {
tokenRanges.push({start: start, end: end, isWord: isWord})
}
|
Based on https://github.com/timdown/rangy/blob/master/src/modules/rangy-textrange.js#L119
|
createTokenRange
|
javascript
|
ritzyed/ritzy
|
src/core/tokenizer.js
|
https://github.com/ritzyed/ritzy/blob/master/src/core/tokenizer.js
|
Apache-2.0
|
createTokenRange = function(start, end, isWord) {
tokenRanges.push({start: start, end: end, isWord: isWord})
}
|
Based on https://github.com/timdown/rangy/blob/master/src/modules/rangy-textrange.js#L119
|
createTokenRange
|
javascript
|
ritzyed/ritzy
|
src/core/tokenizer.js
|
https://github.com/ritzyed/ritzy/blob/master/src/core/tokenizer.js
|
Apache-2.0
|
constructor() {
this.exportPublicMethods({
getPosition: () => this.getPosition(),
getRemoteCursors: () => this.getRemoteCursors(),
getContents: () => this.getContents(),
getContentsRich: () => this.getContentsRich(),
getContentsHtml: () => this.getContentsHtml(),
getContentsText: () => this.getContentsText(),
getSelection: () => this.getSelection(),
getSelectionRich: () => this.getSelectionRich(),
getSelectionHtml: () => this.getSelectionHtml(),
getSelectionText: () => this.getSelectionText()
})
this.bindActions(EditorActions)
this.on('error', (err) => {
console.group('Error Handler')
console.error('Error during dispatch, please report this to https://github.com/ritzyed/ritzy/issues, ideally with a reproduction recipe.')
console.log(err.error.message)
console.log(err.error.stack)
console.log('Payload:')
console.dir(err.payload)
console.log('State:')
console.dir(_.clone(err.state)) // not sure why we need the clone here, err.state.error is updated if not
console.groupEnd()
this.registerEditorError(err.error)
this.emitChange()
throw err.error
})
this.config = null
this.replica = null
this.state = {
position: BASE_CHAR,
positionEolStart: true,
cursorMotion: false,
selectionActive: false,
remoteCursors: {},
remoteNameReveal: new Set(),
activeAttributes: {},
focus: false,
loaded: false,
lines: [],
error: null
}
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
constructor
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
initialize({config, replica}) {
TextFontMetrics.setConfig(config)
this.config = config
this.replica = replica
this.setState({focus: config.initialFocus})
// store re-initialized with new config, reset the cursor model name
// don't bother checking if it is different, remotes will just get the same model again
if(this.cursorModelUpdate) {
this._setRemoteCursorModelName()
}
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
initialize
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
onCursorModelUpdate(cursorModelUpdate) {
this.cursorModelUpdate = cursorModelUpdate
this._setRemoteCursorModelName()
this._setRemoteCursorModelState()
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
onCursorModelUpdate
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
replicaInitialized() {
this.setState({loaded: true})
this._flow()
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
replicaInitialized
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
replicaUpdated() {
// TODO can we determine what changed and do a more focused flow via the modifications parameter?
this._flow()
// update local position and selection if any, and remote selections if any
if(this.state.selectionActive) {
this._updateSelection(this.state, updatedState => {
this.setState(updatedState)
// remotes update our selection model locally to avoid temporary flashing, but send it anyway
this._setRemoteCursorModelState()
})
} else {
let possiblePosition = this._relativeChar(this.state.position, 0)
if(!charEq(this.state.position, possiblePosition)) {
this.setState({
position: possiblePosition
})
this._setRemoteCursorModelState()
}
}
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
replicaUpdated
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
focusInput() {
this._delayedCursorBlink(0)
this.setState({focus: true})
this.config.eventEmitter.emit('focus-gained')
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
focusInput
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
inputFocusLost() {
this.setState({focus: false})
this.config.eventEmitter.emit('focus-lost')
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
inputFocusLost
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
setRemoteCursorPosition(remoteCursor) {
let id = remoteCursor._id
// some bug in Swarm.js: sometimes the id is blank if a lot of cursor events have happened quickly in succession
if(!id) return
// TODO use immutable data structure instead, need clone here to avoid mutating the existing state
let remoteCursors = _.clone(this.state.remoteCursors)
let existingCursor = remoteCursors.hasOwnProperty(id)
if(this.config.eventEmitter.hasListeners('remote-cursor-add') && !existingCursor) {
this.config.eventEmitter.emit('remote-cursor-add', remoteCursor)
}
if(this.config.eventEmitter.hasListeners('remote-cursor-change-name') && existingCursor) {
if(remoteCursors[id].name !== remoteCursor.name) {
this.config.eventEmitter.emit('remote-cursor-change-name', remoteCursor, remoteCursors[id].name, remoteCursor.name)
}
}
remoteCursors[id] = remoteCursor
this.setState({remoteCursors: remoteCursors})
this._delayedRemoteCursorNameReveal(id)
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
setRemoteCursorPosition
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
unsetRemoteCursorPosition(remoteCursor) {
let id = remoteCursor._id
// some bug in Swarm.js: sometimes the id is blank if a lot of cursor events have happened quickly in succession
if(!id) return
// TODO use immutable data structure instead, need clone here to avoid mutating the existing state
let remoteCursors = _.clone(this.state.remoteCursors)
if(id in remoteCursors) {
if(this.config.eventEmitter.hasListeners('remote-cursor-remove')) {
this.config.eventEmitter.emit('remote-cursor-remove', remoteCursor)
}
delete remoteCursors[id]
this.setState({remoteCursors: remoteCursors})
}
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
unsetRemoteCursorPosition
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
getPosition() {
return {
char: this.state.position,
eolStart: this.state.positionEolStart
}
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
getPosition
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
getRemoteCursors() {
return Object.values(this.state.remoteCursors)
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
getRemoteCursors
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
getContents() {
return this.replica.getTextRange(BASE_CHAR)
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
getContents
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
getContentsRich() {
return this._getContentRich(this.getContents())
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
getContentsRich
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
getContentsHtml() {
return writeHtml(this.getContentsRich())
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
getContentsHtml
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
getContentsText() {
return writeText(this.getContentsRich())
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
getContentsText
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
navigatePageUp() {
// assume for now a page is 10 lines
this._navigateUpDown(-10)
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
navigatePageUp
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
navigatePageDown() {
// assume for now a page is 10 lines
this._navigateUpDown(10)
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
navigatePageDown
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
navigateStartLine() {
if(this._emptyEditor()) {
this._resetPosition()
return
}
let {line} = lineContainingChar(this.state.lines, this.state.position, this.state.positionEolStart)
this._setPosition(line.start, true)
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
navigateStartLine
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
navigateEnd() {
if(this._emptyEditor()) {
this._resetPosition()
return
}
let positionEolStart = false
if(this._lastLine().isEof()) {
positionEolStart = true
}
this._setPosition(this._relativeChar(BASE_CHAR, -1), positionEolStart)
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
navigateEnd
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
navigateEndLine() {
if(this._emptyEditor()) {
this._resetPosition()
return
}
let {line} = lineContainingChar(this.state.lines, this.state.position, this.state.positionEolStart)
let position
let positionEolStart = false
if(line.isEof() || line.chunks.length === 0) {
position = this.state.position
positionEolStart = true
} else if (line.isHard()) {
position = this._relativeChar(line.end, -1, 'limit')
} else {
position = line.end
}
this._setPosition(position, positionEolStart)
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
navigateEndLine
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
navigateToCoordinates(coordinates) {
let {position, positionEolStart} = this._coordinatesToPosition(coordinates)
// set the position and selection anchor if the user continues the selection later
position = position ? position : BASE_CHAR
this._setPosition(position, positionEolStart)
this.setState({
selectionAnchorChar: position
})
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
navigateToCoordinates
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
selectionPageUp() {
// assume for now a page is 10 lines
this._selectionUpDown(-10)
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
selectionPageUp
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
selectionPageDown() {
// assume for now a page is 10 lines
this._selectionUpDown(10)
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
selectionPageDown
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
selectionStart() {
this._modifySelection(BASE_CHAR, true)
this.setActiveAttributes()
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
selectionStart
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
selectionStartLine() {
let {line} = lineContainingChar(this.state.lines, this.state.position, this.state.positionEolStart)
this._modifySelection(line.start, true)
this.setActiveAttributes()
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
selectionStartLine
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
selectionEnd() {
let toChar = this._lastLine() && this._lastLine().isEof() ? EOF : this._relativeChar(BASE_CHAR, -1)
this._modifySelection(toChar, toChar === EOF)
this.setActiveAttributes()
}
|
The EditorStore is the editor's single source of truth for application state and logic related to the editor.
View state updates are provided to the view components in setState callbacks. This uses the Facebook Flux
unidirectional flow idea: actions (from the view and elsewhere) -> dispatcher -> store -> view.
See https://facebook.github.io/flux/docs/overview.html
|
selectionEnd
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
selectionEndLine() {
let {line} = lineContainingChar(this.state.lines, this.state.position, this.state.positionEolStart)
let toChar
let positionEolStart = false
if(line.isEof()) {
toChar = this.state.position
positionEolStart = true
} else if (line.isHard()) {
toChar = this._relativeChar(line.end, -1, 'limit')
} else {
toChar = line.end
}
this._modifySelection(toChar, positionEolStart)
this.setActiveAttributes()
}
|
Google docs behavior (as of 2015-06-22) is:
- line with soft return, select line with space at end
- line with hard return, select line without selecting the hard return (no action on empty lines)
- EOF line, no action
Word 2010 behavior is:
- line with soft return, select line with space at end
- line with hard return, select line including the hard return (same on empty lines)
- EOF line, show "space" selection at EOF containing a newline
We implement the Google docs behavior here, which seems a bit more intuitive.
|
selectionEndLine
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
selectionWordLeft() {
let position
let positionEolStart
if(this.state.position === EOF) {
position = this._lastLine().start
positionEolStart = true
} else {
position = this._wordStartRelativeTo(this.state.position)
positionEolStart = !lineContainingChar(this.state.lines, this.state.position).endOfLine
}
this._modifySelection(position, positionEolStart)
this.setActiveAttributes()
}
|
Google docs behavior (as of 2015-06-22) is:
- line with soft return, select line with space at end
- line with hard return, select line without selecting the hard return (no action on empty lines)
- EOF line, no action
Word 2010 behavior is:
- line with soft return, select line with space at end
- line with hard return, select line including the hard return (same on empty lines)
- EOF line, show "space" selection at EOF containing a newline
We implement the Google docs behavior here, which seems a bit more intuitive.
|
selectionWordLeft
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
selectionWordRight() {
let position = this._wordEndRelativeTo(this.state.position)
let endOfLine = lineContainingChar(this.state.lines, this.state.position).endOfLine
this._modifySelection(position, !endOfLine)
this.setActiveAttributes()
}
|
Google docs behavior (as of 2015-06-22) is:
- line with soft return, select line with space at end
- line with hard return, select line without selecting the hard return (no action on empty lines)
- EOF line, no action
Word 2010 behavior is:
- line with soft return, select line with space at end
- line with hard return, select line including the hard return (same on empty lines)
- EOF line, show "space" selection at EOF containing a newline
We implement the Google docs behavior here, which seems a bit more intuitive.
|
selectionWordRight
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
selectionAll() {
this._setPosition(BASE_CHAR)
if(this.state.lines.length === 0) {
this._modifySelection(EOF, true)
} else {
let lastChar = this._lastLine() && this._lastLine().isEof() ? EOF : this._lastLine().end
this._modifySelection(lastChar, false)
}
this.setActiveAttributes()
}
|
Google docs behavior (as of 2015-06-22) is:
- line with soft return, select line with space at end
- line with hard return, select line without selecting the hard return (no action on empty lines)
- EOF line, no action
Word 2010 behavior is:
- line with soft return, select line with space at end
- line with hard return, select line including the hard return (same on empty lines)
- EOF line, show "space" selection at EOF containing a newline
We implement the Google docs behavior here, which seems a bit more intuitive.
|
selectionAll
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
selectToCoordinates(coordinates) {
let {position, positionEolStart} = this._coordinatesToPosition(coordinates)
this._modifySelection(position, positionEolStart)
// we do not setAttributes here, this is handled (for performance reasons) when the mouse up event occurs
}
|
Google docs behavior (as of 2015-06-22) is:
- line with soft return, select line with space at end
- line with hard return, select line without selecting the hard return (no action on empty lines)
- EOF line, no action
Word 2010 behavior is:
- line with soft return, select line with space at end
- line with hard return, select line including the hard return (same on empty lines)
- EOF line, show "space" selection at EOF containing a newline
We implement the Google docs behavior here, which seems a bit more intuitive.
|
selectToCoordinates
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
selectWordAtCurrentPosition() {
if(this.state.lines.length === 0
|| lineContainingChar(this.state.lines, this.state.position, true).line.isEof()) {
this._setPosition(this.state.position)
this._modifySelection(EOF, true)
} else {
let word = this._wordRelativeTo(this.state.position)
this._setPosition(word.start)
this._modifySelection(word.end, false)
}
this.setActiveAttributes()
}
|
Word selection follows Google Docs and Microsoft Word 2010 behavior of:
- selecting the word that was clicked, and one or more spaces following it
- if clicking at the end of a line with a soft return, the first word on the next line is selected
- if clicking at the end of a line with a hard return, the hard return is selected (shown as a "space")
- if clicking at the end of the last (empty) line, a (non-existent) hard return at EOF is selected (shown as
a "space")
|
selectWordAtCurrentPosition
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
copySelection(copyHandler) {
let selectionChunks = this._getSelectionRich()
if(selectionChunks && selectionChunks.length > 0) {
let copiedText = writeText(selectionChunks)
let copiedHtml = writeHtml(selectionChunks)
copyHandler(copiedText, copiedHtml, selectionChunks)
}
}
|
Word selection follows Google Docs and Microsoft Word 2010 behavior of:
- selecting the word that was clicked, and one or more spaces following it
- if clicking at the end of a line with a soft return, the first word on the next line is selected
- if clicking at the end of a line with a hard return, the hard return is selected (shown as a "space")
- if clicking at the end of the last (empty) line, a (non-existent) hard return at EOF is selected (shown as
a "space")
|
copySelection
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
getSelection() {
if(!this.state.selectionActive) return null
return this.replica.getTextRange(this.state.selectionLeftChar, this.state.selectionRightChar)
}
|
Word selection follows Google Docs and Microsoft Word 2010 behavior of:
- selecting the word that was clicked, and one or more spaces following it
- if clicking at the end of a line with a soft return, the first word on the next line is selected
- if clicking at the end of a line with a hard return, the hard return is selected (shown as a "space")
- if clicking at the end of the last (empty) line, a (non-existent) hard return at EOF is selected (shown as
a "space")
|
getSelection
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
getSelectionRich() {
return this._getSelectionRich()
}
|
Word selection follows Google Docs and Microsoft Word 2010 behavior of:
- selecting the word that was clicked, and one or more spaces following it
- if clicking at the end of a line with a soft return, the first word on the next line is selected
- if clicking at the end of a line with a hard return, the hard return is selected (shown as a "space")
- if clicking at the end of the last (empty) line, a (non-existent) hard return at EOF is selected (shown as
a "space")
|
getSelectionRich
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
getSelectionHtml() {
return writeHtml(this._getSelectionRich())
}
|
Word selection follows Google Docs and Microsoft Word 2010 behavior of:
- selecting the word that was clicked, and one or more spaces following it
- if clicking at the end of a line with a soft return, the first word on the next line is selected
- if clicking at the end of a line with a hard return, the hard return is selected (shown as a "space")
- if clicking at the end of the last (empty) line, a (non-existent) hard return at EOF is selected (shown as
a "space")
|
getSelectionHtml
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
getSelectionText() {
return writeText(this._getSelectionRich())
}
|
Word selection follows Google Docs and Microsoft Word 2010 behavior of:
- selecting the word that was clicked, and one or more spaces following it
- if clicking at the end of a line with a soft return, the first word on the next line is selected
- if clicking at the end of a line with a hard return, the hard return is selected (shown as a "space")
- if clicking at the end of the last (empty) line, a (non-existent) hard return at EOF is selected (shown as
a "space")
|
getSelectionText
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
insertChars({value, attributes, atPosition, reflow}) {
return this._insertChars(value, attributes, atPosition, reflow)
}
|
Word selection follows Google Docs and Microsoft Word 2010 behavior of:
- selecting the word that was clicked, and one or more spaces following it
- if clicking at the end of a line with a soft return, the first word on the next line is selected
- if clicking at the end of a line with a hard return, the hard return is selected (shown as a "space")
- if clicking at the end of the last (empty) line, a (non-existent) hard return at EOF is selected (shown as
a "space")
|
insertChars
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
insertCharsBatch(chunks) {
let firstInsertPosition = this.state.position
let insertPosition = null
chunks.forEach(c => {
insertPosition = this._insertChars(c.text, c.attrs || null, insertPosition, false)
})
this._flow({start: firstInsertPosition, end: insertPosition, action: 'insert'})
}
|
Word selection follows Google Docs and Microsoft Word 2010 behavior of:
- selecting the word that was clicked, and one or more spaces following it
- if clicking at the end of a line with a soft return, the first word on the next line is selected
- if clicking at the end of a line with a hard return, the hard return is selected (shown as a "space")
- if clicking at the end of the last (empty) line, a (non-existent) hard return at EOF is selected (shown as
a "space")
|
insertCharsBatch
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
eraseCharBack() {
if(this.state.selectionActive) {
this._eraseSelection()
} else if(!charEq(this.state.position, BASE_CHAR)) {
let position = this._relativeChar(this.state.position, -1)
this.replica.rmChars(this.state.position)
this._flow({ start: position, end: this.state.position, action: ACTION_DELETE})
if(this.config.eventEmitter.hasListeners('text-delete')) {
this.config.eventEmitter.emit('text-delete', position, this.state.position, position)
}
let endOfLine = lineContainingChar(this.state.lines, position).endOfLine
this._setPosition(position, endOfLine)
this._updateRemoteCursorSelectionsLocally()
}
}
|
Word selection follows Google Docs and Microsoft Word 2010 behavior of:
- selecting the word that was clicked, and one or more spaces following it
- if clicking at the end of a line with a soft return, the first word on the next line is selected
- if clicking at the end of a line with a hard return, the hard return is selected (shown as a "space")
- if clicking at the end of the last (empty) line, a (non-existent) hard return at EOF is selected (shown as
a "space")
|
eraseCharBack
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
eraseCharForward() {
if(this.state.selectionActive) {
this._eraseSelection()
} else if(!this._cursorAtEnd()) {
let next = this._relativeChar(this.state.position, 1, 'limit')
this.replica.rmChars(next)
this._flow({ start: this.state.position, end: next, action: ACTION_DELETE})
if(this.config.eventEmitter.hasListeners('text-delete')) {
this.config.eventEmitter.emit('text-delete', this.state.position, next, this.state.position)
}
let endOfLine = lineContainingChar(this.state.lines, this.state.position).endOfLine
this._setPosition(this.state.position, endOfLine)
this._updateRemoteCursorSelectionsLocally()
}
}
|
Word selection follows Google Docs and Microsoft Word 2010 behavior of:
- selecting the word that was clicked, and one or more spaces following it
- if clicking at the end of a line with a soft return, the first word on the next line is selected
- if clicking at the end of a line with a hard return, the hard return is selected (shown as a "space")
- if clicking at the end of the last (empty) line, a (non-existent) hard return at EOF is selected (shown as
a "space")
|
eraseCharForward
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
eraseWordBack() {
if(this.state.selectionActive) {
this._eraseSelection()
} else {
let position = this.state.position
let start = this._wordStartRelativeTo(position)
let end = position
if(charEq(start, position)) {
// beginning of word, move to the previous word
let previousStart = this._wordStartRelativeTo(this._relativeChar(position, -1, 'limit'))
// no previous word, nothing to delete
if(start === previousStart) return
end = this._wordEndRelativeTo(start)
}
// TODO delete at beginning of line deletes last word on previous line or last word of previous paragraph
let wordChars = this.replica.getTextRange(start, end)
this.replica.rmChars(wordChars)
this._flow({ start: start, end: end, action: ACTION_DELETE})
if(this.config.eventEmitter.hasListeners('text-delete')) {
this.config.eventEmitter.emit('text-delete', start, end, start)
}
let lineContainingStart = lineContainingChar(this.state.lines, start)
let endOfLine = lineContainingStart ? lineContainingStart.endOfLine : true
this._setPosition(start, endOfLine)
this._updateRemoteCursorSelectionsLocally()
}
}
|
Word selection follows Google Docs and Microsoft Word 2010 behavior of:
- selecting the word that was clicked, and one or more spaces following it
- if clicking at the end of a line with a soft return, the first word on the next line is selected
- if clicking at the end of a line with a hard return, the hard return is selected (shown as a "space")
- if clicking at the end of the last (empty) line, a (non-existent) hard return at EOF is selected (shown as
a "space")
|
eraseWordBack
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
eraseWordForward() {
if(this.state.selectionActive) {
this._eraseSelection()
} else {
let position = this.state.position
let options
if(isWhitespace(this._relativeChar(position, 1, 'limit').char)) {
options = { includeLeadingSpace: true }
}
let start = position
let end = this._wordEndRelativeTo(start, options)
if(charEq(end, position)) {
// ending of word, move to the next word
let nextEnd = this._wordEndRelativeTo(this._relativeChar(position, 1, 'limit'), options)
// no next word, nothing to delete
if(end === nextEnd) return
start = this._wordStartRelativeTo(end, options)
}
// TODO delete at end of line deletes first word on next line or first word of next paragraph
let wordChars = this.replica.getTextRange(start, end)
this.replica.rmChars(wordChars)
this._flow({ start: start, end: end, action: ACTION_DELETE})
if(this.config.eventEmitter.hasListeners('text-delete')) {
this.config.eventEmitter.emit('text-delete', start, end, start)
}
let lineContainingStart = lineContainingChar(this.state.lines, start)
let endOfLine = lineContainingStart ? lineContainingStart.endOfLine : true
this._setPosition(start, endOfLine)
this._updateRemoteCursorSelectionsLocally()
}
}
|
Word selection follows Google Docs and Microsoft Word 2010 behavior of:
- selecting the word that was clicked, and one or more spaces following it
- if clicking at the end of a line with a soft return, the first word on the next line is selected
- if clicking at the end of a line with a hard return, the hard return is selected (shown as a "space")
- if clicking at the end of the last (empty) line, a (non-existent) hard return at EOF is selected (shown as
a "space")
|
eraseWordForward
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
eraseSelection() {
if(this.state.selectionActive) {
this._eraseSelection()
}
}
|
Word selection follows Google Docs and Microsoft Word 2010 behavior of:
- selecting the word that was clicked, and one or more spaces following it
- if clicking at the end of a line with a soft return, the first word on the next line is selected
- if clicking at the end of a line with a hard return, the hard return is selected (shown as a "space")
- if clicking at the end of the last (empty) line, a (non-existent) hard return at EOF is selected (shown as
a "space")
|
eraseSelection
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
toggleSuperscript() {
this._toggleAttribute(ATTR.SUPERSCRIPT, ATTR.SUBSCRIPT)
}
|
Word selection follows Google Docs and Microsoft Word 2010 behavior of:
- selecting the word that was clicked, and one or more spaces following it
- if clicking at the end of a line with a soft return, the first word on the next line is selected
- if clicking at the end of a line with a hard return, the hard return is selected (shown as a "space")
- if clicking at the end of the last (empty) line, a (non-existent) hard return at EOF is selected (shown as
a "space")
|
toggleSuperscript
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
toggleSubscript() {
this._toggleAttribute(ATTR.SUBSCRIPT, ATTR.SUPERSCRIPT)
}
|
Word selection follows Google Docs and Microsoft Word 2010 behavior of:
- selecting the word that was clicked, and one or more spaces following it
- if clicking at the end of a line with a soft return, the first word on the next line is selected
- if clicking at the end of a line with a hard return, the hard return is selected (shown as a "space")
- if clicking at the end of the last (empty) line, a (non-existent) hard return at EOF is selected (shown as
a "space")
|
toggleSubscript
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
setActiveAttributes() {
let attributes
if(this.state.selectionActive) {
// activeAttributes are set by the common selection attributes
let selection = this.getSelection()
attributes = selection.length > 0 ? selection[0].copyOfAttributes() : null
if(attributes && selection.length > 1) {
for(let i = 1; i < selection.length && !_.isEmpty(attributes); i++) {
let char = selection[i]
let charAttrs = char.attributes
if(!charAttrs) {
attributes = null
break
} else {
Object.keys(attributes).forEach(attr => {
if(attributes[attr] && !charAttrs[attr]) delete attributes[attr]
})
}
}
}
} else {
attributes = this.state.position.attributes
}
this.setState({activeAttributes: attributes})
}
|
Word selection follows Google Docs and Microsoft Word 2010 behavior of:
- selecting the word that was clicked, and one or more spaces following it
- if clicking at the end of a line with a soft return, the first word on the next line is selected
- if clicking at the end of a line with a hard return, the hard return is selected (shown as a "space")
- if clicking at the end of the last (empty) line, a (non-existent) hard return at EOF is selected (shown as
a "space")
|
setActiveAttributes
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
registerEditorError(error) {
this.setState({error: error})
}
|
Word selection follows Google Docs and Microsoft Word 2010 behavior of:
- selecting the word that was clicked, and one or more spaces following it
- if clicking at the end of a line with a soft return, the first word on the next line is selected
- if clicking at the end of a line with a hard return, the hard return is selected (shown as a "space")
- if clicking at the end of the last (empty) line, a (non-existent) hard return at EOF is selected (shown as
a "space")
|
registerEditorError
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
dismissEditorError() {
this.setState({error: null})
}
|
Word selection follows Google Docs and Microsoft Word 2010 behavior of:
- selecting the word that was clicked, and one or more spaces following it
- if clicking at the end of a line with a soft return, the first word on the next line is selected
- if clicking at the end of a line with a hard return, the hard return is selected (shown as a "space")
- if clicking at the end of the last (empty) line, a (non-existent) hard return at EOF is selected (shown as
a "space")
|
dismissEditorError
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
_insertChars(value, attributes, atPosition, reflow) {
if(_.isUndefined(reflow)) reflow = true
let position
if(!attributes) {
// use the active attributes if attributes are not set explicitly
attributes = this.state.activeAttributes
}
if(this.state.selectionActive) {
position = this.state.selectionLeftChar
this._eraseSelection()
} else {
if(atPosition) {
position = atPosition
} else {
position = this.state.position
}
if(position === EOF) {
position = this._relativeChar(EOF, -1, 'limit')
}
}
// normalize line endings (CRLF -> LF, CR -> LF)
value = value.replace(/\r\n/g, '\n')
value = value.replace(/\r/g, '\n')
this.replica.insertCharsAt(position, value, attributes)
let relativeMove = value.length
let newPosition = this._relativeChar(position, relativeMove)
// if the last char is a newline, then we want to position on the start of the next line
let newPositionEolStart = value.slice(-1) === '\n'
if(reflow) this._flow({start: position, end: newPosition, action: ACTION_INSERT})
if(this.config.eventEmitter.hasListeners('text-insert')) {
this.config.eventEmitter.emit('text-insert', position, value, attributes, newPosition)
}
this._setPosition(newPosition, newPositionEolStart)
this.setState({activeAttributes: attributes})
this._updateRemoteCursorSelectionsLocally()
// return the new position so that multiple insertChars calls can be made in sequence
return newPosition
}
|
Word selection follows Google Docs and Microsoft Word 2010 behavior of:
- selecting the word that was clicked, and one or more spaces following it
- if clicking at the end of a line with a soft return, the first word on the next line is selected
- if clicking at the end of a line with a hard return, the hard return is selected (shown as a "space")
- if clicking at the end of the last (empty) line, a (non-existent) hard return at EOF is selected (shown as
a "space")
|
_insertChars
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
_relativeChar(charOrId, relative, wrap) {
return this.replica.getCharRelativeTo(charOrId, relative, wrap)
}
|
Word selection follows Google Docs and Microsoft Word 2010 behavior of:
- selecting the word that was clicked, and one or more spaces following it
- if clicking at the end of a line with a soft return, the first word on the next line is selected
- if clicking at the end of a line with a hard return, the hard return is selected (shown as a "space")
- if clicking at the end of the last (empty) line, a (non-existent) hard return at EOF is selected (shown as
a "space")
|
_relativeChar
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
reset() {
this.chars = []
this.length = 0
this.pendingChunks = []
this.advance = 0
this.lineAdvance = 0
this.attributes = null
this.lastCharSpace = false
}
|
Flows the content i.e. wraps the text into multiple lines, splits each line into chunks with the same
attributes, and sets the component state based on the result. This state is then used for rendering the
editor surface during the next render cycle. This should be called after any operation that may change
the content flow, such as inserting or deleting text.
One or more spaces at the end of a word are not "counted" for wrapping purposes because that would cause the
space to show up at the beginning of the next line. Instead, these spaces are included on the prior line
even though strictly it causes the line length to exceed the margin. This behavior is consistent with
common word processors such as Microsoft Word and Google Docs.
Even though all of the state here can be calculated from the replica, this is not done at render time
because the line state must be available when user input such as clicks or selections are made.
Because the algorithm to shortcut the flow before and after the changed area is complex, and can fail
without causing an obvious problem other than performance, debug statements are included and can be
enabled by setting FLOW_DEBUG to true.
|
reset
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
pushChar(c, fontSize) {
if(!this.attributes) {
this.attributes = c.attributes
}
let charAdvance = TextFontMetrics.advanceXForChars(fontSize, c)
// don't count spaces in the word in the advance, but include it in the line advance
if(c.char === ' ') {
this.lastCharSpace = true
} else {
this.advance += charAdvance
}
this.lineAdvance += charAdvance
this.chars.push(c)
this.length++
}
|
Flows the content i.e. wraps the text into multiple lines, splits each line into chunks with the same
attributes, and sets the component state based on the result. This state is then used for rendering the
editor surface during the next render cycle. This should be called after any operation that may change
the content flow, such as inserting or deleting text.
One or more spaces at the end of a word are not "counted" for wrapping purposes because that would cause the
space to show up at the beginning of the next line. Instead, these spaces are included on the prior line
even though strictly it causes the line length to exceed the margin. This behavior is consistent with
common word processors such as Microsoft Word and Google Docs.
Even though all of the state here can be calculated from the replica, this is not done at render time
because the line state must be available when user input such as clicks or selections are made.
Because the algorithm to shortcut the flow before and after the changed area is complex, and can fail
without causing an obvious problem other than performance, debug statements are included and can be
enabled by setting FLOW_DEBUG to true.
|
pushChar
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
popChar() {
this.length--
return this.chars.pop()
}
|
Flows the content i.e. wraps the text into multiple lines, splits each line into chunks with the same
attributes, and sets the component state based on the result. This state is then used for rendering the
editor surface during the next render cycle. This should be called after any operation that may change
the content flow, such as inserting or deleting text.
One or more spaces at the end of a word are not "counted" for wrapping purposes because that would cause the
space to show up at the beginning of the next line. Instead, these spaces are included on the prior line
even though strictly it causes the line length to exceed the margin. This behavior is consistent with
common word processors such as Microsoft Word and Google Docs.
Even though all of the state here can be calculated from the replica, this is not done at render time
because the line state must be available when user input such as clicks or selections are made.
Because the algorithm to shortcut the flow before and after the changed area is complex, and can fail
without causing an obvious problem other than performance, debug statements are included and can be
enabled by setting FLOW_DEBUG to true.
|
popChar
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
pushChunks() {
if(this.length > 0) {
this.pendingChunks.push({
length: this.length,
attributes: this.attributes
})
}
this.attributes = null
this.length = 0
}
|
Flows the content i.e. wraps the text into multiple lines, splits each line into chunks with the same
attributes, and sets the component state based on the result. This state is then used for rendering the
editor surface during the next render cycle. This should be called after any operation that may change
the content flow, such as inserting or deleting text.
One or more spaces at the end of a word are not "counted" for wrapping purposes because that would cause the
space to show up at the beginning of the next line. Instead, these spaces are included on the prior line
even though strictly it causes the line length to exceed the margin. This behavior is consistent with
common word processors such as Microsoft Word and Google Docs.
Even though all of the state here can be calculated from the replica, this is not done at render time
because the line state must be available when user input such as clicks or selections are made.
Because the algorithm to shortcut the flow before and after the changed area is complex, and can fail
without causing an obvious problem other than performance, debug statements are included and can be
enabled by setting FLOW_DEBUG to true.
|
pushChunks
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
reset() {
this.chars = []
this.chunks = []
this.advance = 0
this.start = null
this.end = null
}
|
Flows the content i.e. wraps the text into multiple lines, splits each line into chunks with the same
attributes, and sets the component state based on the result. This state is then used for rendering the
editor surface during the next render cycle. This should be called after any operation that may change
the content flow, such as inserting or deleting text.
One or more spaces at the end of a word are not "counted" for wrapping purposes because that would cause the
space to show up at the beginning of the next line. Instead, these spaces are included on the prior line
even though strictly it causes the line length to exceed the margin. This behavior is consistent with
common word processors such as Microsoft Word and Google Docs.
Even though all of the state here can be calculated from the replica, this is not done at render time
because the line state must be available when user input such as clicks or selections are made.
Because the algorithm to shortcut the flow before and after the changed area is complex, and can fail
without causing an obvious problem other than performance, debug statements are included and can be
enabled by setting FLOW_DEBUG to true.
|
reset
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
pushWord(word) {
pushArray(this.chars, word.chars)
let lastChunk
if(this.chunks.length > 0) {
lastChunk = this.chunks[this.chunks.length - 1]
// if the last chunk in the line matches attributes with the first word chunk, join them to minimize chunks
if(attributesEqual(lastChunk.attributes, word.pendingChunks[0].attributes)) {
lastChunk.end += word.pendingChunks[0].length
word.pendingChunks.shift()
}
}
// set the start and end of each chunk, start and end are suitable for feeding to chars.slice i.e. end is exclusive
let start = lastChunk ? lastChunk.end : 0
word.pendingChunks.map(c => {
c.start = start
c.end = start + c.length
start = c.end
delete c.length
return c
})
pushArray(this.chunks, word.pendingChunks)
this.end = this.chars[this.chars.length - 1]
this.advance += word.lineAdvance
word.reset()
}
|
Flows the content i.e. wraps the text into multiple lines, splits each line into chunks with the same
attributes, and sets the component state based on the result. This state is then used for rendering the
editor surface during the next render cycle. This should be called after any operation that may change
the content flow, such as inserting or deleting text.
One or more spaces at the end of a word are not "counted" for wrapping purposes because that would cause the
space to show up at the beginning of the next line. Instead, these spaces are included on the prior line
even though strictly it causes the line length to exceed the margin. This behavior is consistent with
common word processors such as Microsoft Word and Google Docs.
Even though all of the state here can be calculated from the replica, this is not done at render time
because the line state must be available when user input such as clicks or selections are made.
Because the algorithm to shortcut the flow before and after the changed area is complex, and can fail
without causing an obvious problem other than performance, debug statements are included and can be
enabled by setting FLOW_DEBUG to true.
|
pushWord
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
pushNewline(c) {
invariant(c.char === '\n', 'pushNewline can only be called with a newline char.')
this.chars.push(c)
this.end = c
}
|
Flows the content i.e. wraps the text into multiple lines, splits each line into chunks with the same
attributes, and sets the component state based on the result. This state is then used for rendering the
editor surface during the next render cycle. This should be called after any operation that may change
the content flow, such as inserting or deleting text.
One or more spaces at the end of a word are not "counted" for wrapping purposes because that would cause the
space to show up at the beginning of the next line. Instead, these spaces are included on the prior line
even though strictly it causes the line length to exceed the margin. This behavior is consistent with
common word processors such as Microsoft Word and Google Docs.
Even though all of the state here can be calculated from the replica, this is not done at render time
because the line state must be available when user input such as clicks or selections are made.
Because the algorithm to shortcut the flow before and after the changed area is complex, and can fail
without causing an obvious problem other than performance, debug statements are included and can be
enabled by setting FLOW_DEBUG to true.
|
pushNewline
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
pushEof() {
this.end = EOF
}
|
Flows the content i.e. wraps the text into multiple lines, splits each line into chunks with the same
attributes, and sets the component state based on the result. This state is then used for rendering the
editor surface during the next render cycle. This should be called after any operation that may change
the content flow, such as inserting or deleting text.
One or more spaces at the end of a word are not "counted" for wrapping purposes because that would cause the
space to show up at the beginning of the next line. Instead, these spaces are included on the prior line
even though strictly it causes the line length to exceed the margin. This behavior is consistent with
common word processors such as Microsoft Word and Google Docs.
Even though all of the state here can be calculated from the replica, this is not done at render time
because the line state must be available when user input such as clicks or selections are made.
Because the algorithm to shortcut the flow before and after the changed area is complex, and can fail
without causing an obvious problem other than performance, debug statements are included and can be
enabled by setting FLOW_DEBUG to true.
|
pushEof
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
pushLine = (line) => {
if(line.end) {
let l = new Line(line.chars, line.chunks, line.start, line.end, line.advance)
lines.push(l)
if(FLOW_DEBUG) {
console.debug(`PUSHED LINE (index=${lines.length - 1}): ${l.toString()}`)
}
newLine = true
}
line.reset()
line.start = lines.length > 0 ? lines[lines.length - 1].end : BASE_CHAR
}
|
Flows the content i.e. wraps the text into multiple lines, splits each line into chunks with the same
attributes, and sets the component state based on the result. This state is then used for rendering the
editor surface during the next render cycle. This should be called after any operation that may change
the content flow, such as inserting or deleting text.
One or more spaces at the end of a word are not "counted" for wrapping purposes because that would cause the
space to show up at the beginning of the next line. Instead, these spaces are included on the prior line
even though strictly it causes the line length to exceed the margin. This behavior is consistent with
common word processors such as Microsoft Word and Google Docs.
Even though all of the state here can be calculated from the replica, this is not done at render time
because the line state must be available when user input such as clicks or selections are made.
Because the algorithm to shortcut the flow before and after the changed area is complex, and can fail
without causing an obvious problem other than performance, debug statements are included and can be
enabled by setting FLOW_DEBUG to true.
|
pushLine
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
pushLine = (line) => {
if(line.end) {
let l = new Line(line.chars, line.chunks, line.start, line.end, line.advance)
lines.push(l)
if(FLOW_DEBUG) {
console.debug(`PUSHED LINE (index=${lines.length - 1}): ${l.toString()}`)
}
newLine = true
}
line.reset()
line.start = lines.length > 0 ? lines[lines.length - 1].end : BASE_CHAR
}
|
Flows the content i.e. wraps the text into multiple lines, splits each line into chunks with the same
attributes, and sets the component state based on the result. This state is then used for rendering the
editor surface during the next render cycle. This should be called after any operation that may change
the content flow, such as inserting or deleting text.
One or more spaces at the end of a word are not "counted" for wrapping purposes because that would cause the
space to show up at the beginning of the next line. Instead, these spaces are included on the prior line
even though strictly it causes the line length to exceed the margin. This behavior is consistent with
common word processors such as Microsoft Word and Google Docs.
Even though all of the state here can be calculated from the replica, this is not done at render time
because the line state must be available when user input such as clicks or selections are made.
Because the algorithm to shortcut the flow before and after the changed area is complex, and can fail
without causing an obvious problem other than performance, debug statements are included and can be
enabled by setting FLOW_DEBUG to true.
|
pushLine
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
_setRemoteCursorModelName() {
if(!this.cursorModelUpdate) return
let updatedModel = {
name: this.config.userName,
ms: Date.now()
}
this.cursorModelUpdate(updatedModel)
}
|
Flows the content i.e. wraps the text into multiple lines, splits each line into chunks with the same
attributes, and sets the component state based on the result. This state is then used for rendering the
editor surface during the next render cycle. This should be called after any operation that may change
the content flow, such as inserting or deleting text.
One or more spaces at the end of a word are not "counted" for wrapping purposes because that would cause the
space to show up at the beginning of the next line. Instead, these spaces are included on the prior line
even though strictly it causes the line length to exceed the margin. This behavior is consistent with
common word processors such as Microsoft Word and Google Docs.
Even though all of the state here can be calculated from the replica, this is not done at render time
because the line state must be available when user input such as clicks or selections are made.
Because the algorithm to shortcut the flow before and after the changed area is complex, and can fail
without causing an obvious problem other than performance, debug statements are included and can be
enabled by setting FLOW_DEBUG to true.
|
_setRemoteCursorModelName
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
_setRemoteCursorModelState() {
if(!this.cursorModelUpdate) return
let updatedModel = {
state: {
position: this.state.position.id,
positionEolStart: this.state.positionEolStart,
selectionActive: this.state.selectionActive,
selectionLeftChar: this.state.selectionActive ? charId(this.state.selectionLeftChar) : null,
selectionRightChar: this.state.selectionActive ? charId(this.state.selectionRightChar) : null
},
ms: Date.now()
}
this.cursorModelUpdate(updatedModel)
}
|
Flows the content i.e. wraps the text into multiple lines, splits each line into chunks with the same
attributes, and sets the component state based on the result. This state is then used for rendering the
editor surface during the next render cycle. This should be called after any operation that may change
the content flow, such as inserting or deleting text.
One or more spaces at the end of a word are not "counted" for wrapping purposes because that would cause the
space to show up at the beginning of the next line. Instead, these spaces are included on the prior line
even though strictly it causes the line length to exceed the margin. This behavior is consistent with
common word processors such as Microsoft Word and Google Docs.
Even though all of the state here can be calculated from the replica, this is not done at render time
because the line state must be available when user input such as clicks or selections are made.
Because the algorithm to shortcut the flow before and after the changed area is complex, and can fail
without causing an obvious problem other than performance, debug statements are included and can be
enabled by setting FLOW_DEBUG to true.
|
_setRemoteCursorModelState
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
_setPosition(position, positionEolStart, resetUpDown) {
if(_.isUndefined(positionEolStart)) positionEolStart = this.state.positionEolStart
if(_.isUndefined(resetUpDown)) resetUpDown = true
//console.debug('position', position, 'positionEolStart', positionEolStart, 'resetUpDown', resetUpDown)
let currentPosition = this.state.position
let selectionWasActive = this.state.selectionActive
this.setState({
position: position,
positionEolStart: positionEolStart,
selectionActive: false
})
if(!charEq(position, currentPosition) || selectionWasActive) {
this.setState({ activeAttributes: position.attributes })
}
if(resetUpDown) {
this.upDownAdvanceX = null
this.upDownPositionEolStart = null
}
this._delayedCursorBlink()
this._setRemoteCursorModelState()
if(this.config.eventEmitter.hasListeners('position-change')) {
this.config.eventEmitter.emit('position-change', this.getPosition())
}
if(selectionWasActive && this.config.eventEmitter.hasListeners('selection-change')) {
this.config.eventEmitter.emit('selection-change', null)
}
}
|
Sets the character and cursor position within the text. The position is relative to an existing
character given by its replica id. The cursor position is calculated based on the character
position. This is generally straightforward except when the character position is the last
character of a line. In this situation, there are two possible cursor positions: at the end of
the line, or at the beginning of the next line. The desired cursor location depends on how one
got there e.g. hitting "end" on a line should keep you on the same line, hitting "home" on the
next line takes you to the same character position, except at the beginning of that line.
@param {object} position The character position to set.
@param {boolean} [positionEolStart = true] positionEolStart When rendering the cursor, this state
determines the cursor position when the character position is at a line end: whether to place
the cursor at the start of the next line (positionEolStart = true), or at the end of the
current one (positionEolStart = false). If the cursor position is not at a line end, this state
is ignored by the renderer. Since this state is often "left over" from previous calls to _setPosition
it should not be trusted other than for rendering.
@param {boolean} [resetUpDown = true] resetUpDown Whether to reset the up/down advance
and position values.
|
_setPosition
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
_delayedCursorBlink(timeout) {
if(_.isUndefined(timeout)) timeout = 1000
this.setState({cursorMotion: true})
// after timeout, reset the cursor blink, clear any previous resets to avoid unnecessary state changes
if(this.cursorMotionTimeout) {
clearTimeout(this.cursorMotionTimeout)
}
this.cursorMotionTimeout = setTimeout(() => {
this.setState({cursorMotion: false})
this.cursorMotionTimeout = null
}, timeout)
}
|
Sets the character and cursor position within the text. The position is relative to an existing
character given by its replica id. The cursor position is calculated based on the character
position. This is generally straightforward except when the character position is the last
character of a line. In this situation, there are two possible cursor positions: at the end of
the line, or at the beginning of the next line. The desired cursor location depends on how one
got there e.g. hitting "end" on a line should keep you on the same line, hitting "home" on the
next line takes you to the same character position, except at the beginning of that line.
@param {object} position The character position to set.
@param {boolean} [positionEolStart = true] positionEolStart When rendering the cursor, this state
determines the cursor position when the character position is at a line end: whether to place
the cursor at the start of the next line (positionEolStart = true), or at the end of the
current one (positionEolStart = false). If the cursor position is not at a line end, this state
is ignored by the renderer. Since this state is often "left over" from previous calls to _setPosition
it should not be trusted other than for rendering.
@param {boolean} [resetUpDown = true] resetUpDown Whether to reset the up/down advance
and position values.
|
_delayedCursorBlink
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
_delayedRemoteCursorNameReveal(id, timeout) {
if(_.isUndefined(timeout)) timeout = 2000
// TODO use immutable data structure instead, need to create a new Set here to avoid mutating the existing state
let remoteNameReveal = new Set(this.state.remoteNameReveal)
remoteNameReveal.add(id)
this.setState({remoteNameReveal: remoteNameReveal})
setTimeout(() => {
// this should never be false
let remoteNameReveal = new Set(this.state.remoteNameReveal)
remoteNameReveal.delete(id)
this.setState({remoteNameReveal: remoteNameReveal})
}, timeout)
}
|
Sets the character and cursor position within the text. The position is relative to an existing
character given by its replica id. The cursor position is calculated based on the character
position. This is generally straightforward except when the character position is the last
character of a line. In this situation, there are two possible cursor positions: at the end of
the line, or at the beginning of the next line. The desired cursor location depends on how one
got there e.g. hitting "end" on a line should keep you on the same line, hitting "home" on the
next line takes you to the same character position, except at the beginning of that line.
@param {object} position The character position to set.
@param {boolean} [positionEolStart = true] positionEolStart When rendering the cursor, this state
determines the cursor position when the character position is at a line end: whether to place
the cursor at the start of the next line (positionEolStart = true), or at the end of the
current one (positionEolStart = false). If the cursor position is not at a line end, this state
is ignored by the renderer. Since this state is often "left over" from previous calls to _setPosition
it should not be trusted other than for rendering.
@param {boolean} [resetUpDown = true] resetUpDown Whether to reset the up/down advance
and position values.
|
_delayedRemoteCursorNameReveal
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
_lastLine() {
return this.state.lines[this.state.lines.length - 1]
}
|
Sets the character and cursor position within the text. The position is relative to an existing
character given by its replica id. The cursor position is calculated based on the character
position. This is generally straightforward except when the character position is the last
character of a line. In this situation, there are two possible cursor positions: at the end of
the line, or at the beginning of the next line. The desired cursor location depends on how one
got there e.g. hitting "end" on a line should keep you on the same line, hitting "home" on the
next line takes you to the same character position, except at the beginning of that line.
@param {object} position The character position to set.
@param {boolean} [positionEolStart = true] positionEolStart When rendering the cursor, this state
determines the cursor position when the character position is at a line end: whether to place
the cursor at the start of the next line (positionEolStart = true), or at the end of the
current one (positionEolStart = false). If the cursor position is not at a line end, this state
is ignored by the renderer. Since this state is often "left over" from previous calls to _setPosition
it should not be trusted other than for rendering.
@param {boolean} [resetUpDown = true] resetUpDown Whether to reset the up/down advance
and position values.
|
_lastLine
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
_emptyEditor() {
return this.state.lines.length === 0
&& (this.state.position === EOF || charEq(this.state.position, BASE_CHAR))
}
|
Sets the character and cursor position within the text. The position is relative to an existing
character given by its replica id. The cursor position is calculated based on the character
position. This is generally straightforward except when the character position is the last
character of a line. In this situation, there are two possible cursor positions: at the end of
the line, or at the beginning of the next line. The desired cursor location depends on how one
got there e.g. hitting "end" on a line should keep you on the same line, hitting "home" on the
next line takes you to the same character position, except at the beginning of that line.
@param {object} position The character position to set.
@param {boolean} [positionEolStart = true] positionEolStart When rendering the cursor, this state
determines the cursor position when the character position is at a line end: whether to place
the cursor at the start of the next line (positionEolStart = true), or at the end of the
current one (positionEolStart = false). If the cursor position is not at a line end, this state
is ignored by the renderer. Since this state is often "left over" from previous calls to _setPosition
it should not be trusted other than for rendering.
@param {boolean} [resetUpDown = true] resetUpDown Whether to reset the up/down advance
and position values.
|
_emptyEditor
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
_cursorAtEnd() {
return this._emptyEditor()
|| charEq(this.state.position, this._lastLine().end)
|| (this._lastLine().isEof() && charEq(this.state.position, this._lastLine().start))
}
|
Sets the character and cursor position within the text. The position is relative to an existing
character given by its replica id. The cursor position is calculated based on the character
position. This is generally straightforward except when the character position is the last
character of a line. In this situation, there are two possible cursor positions: at the end of
the line, or at the beginning of the next line. The desired cursor location depends on how one
got there e.g. hitting "end" on a line should keep you on the same line, hitting "home" on the
next line takes you to the same character position, except at the beginning of that line.
@param {object} position The character position to set.
@param {boolean} [positionEolStart = true] positionEolStart When rendering the cursor, this state
determines the cursor position when the character position is at a line end: whether to place
the cursor at the start of the next line (positionEolStart = true), or at the end of the
current one (positionEolStart = false). If the cursor position is not at a line end, this state
is ignored by the renderer. Since this state is often "left over" from previous calls to _setPosition
it should not be trusted other than for rendering.
@param {boolean} [resetUpDown = true] resetUpDown Whether to reset the up/down advance
and position values.
|
_cursorAtEnd
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
_getContentRich(chars) {
let contentChunks = []
let currentChunk = {
chars: [],
attributes: null,
reset() {
this.chars = []
this.attributes = null
},
pushChar(c) {
if(!this.attributes) {
this.attributes = c.attributes
}
// push newlines as separate chunks for ease of parsing paragraphs and breaks from chunks
let isNewline = c.char === '\n'
if(isNewline) {
// previous chunk
this.pushChunk()
}
this.chars.push(c.char)
if(isNewline) {
// newline chunk
this.pushChunk()
}
},
pushChunk() {
if(this.chars.length > 0) {
contentChunks.push({
text: this.chars.join(''),
attrs: this.attributes
})
}
this.reset()
}
}
let processChar = (c) => {
if (!attributesEqual(currentChunk.attributes, c.attributes)) {
currentChunk.pushChunk()
}
currentChunk.pushChar(c, this)
}
let contentIterator = chars[Symbol.iterator]()
let e
while(!(e = contentIterator.next()).done) {
processChar(e.value)
}
// last chunk
if(currentChunk.chars.length > 0) currentChunk.pushChunk()
return contentChunks
}
|
Sets the character and cursor position within the text. The position is relative to an existing
character given by its replica id. The cursor position is calculated based on the character
position. This is generally straightforward except when the character position is the last
character of a line. In this situation, there are two possible cursor positions: at the end of
the line, or at the beginning of the next line. The desired cursor location depends on how one
got there e.g. hitting "end" on a line should keep you on the same line, hitting "home" on the
next line takes you to the same character position, except at the beginning of that line.
@param {object} position The character position to set.
@param {boolean} [positionEolStart = true] positionEolStart When rendering the cursor, this state
determines the cursor position when the character position is at a line end: whether to place
the cursor at the start of the next line (positionEolStart = true), or at the end of the
current one (positionEolStart = false). If the cursor position is not at a line end, this state
is ignored by the renderer. Since this state is often "left over" from previous calls to _setPosition
it should not be trusted other than for rendering.
@param {boolean} [resetUpDown = true] resetUpDown Whether to reset the up/down advance
and position values.
|
_getContentRich
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
reset() {
this.chars = []
this.attributes = null
}
|
Sets the character and cursor position within the text. The position is relative to an existing
character given by its replica id. The cursor position is calculated based on the character
position. This is generally straightforward except when the character position is the last
character of a line. In this situation, there are two possible cursor positions: at the end of
the line, or at the beginning of the next line. The desired cursor location depends on how one
got there e.g. hitting "end" on a line should keep you on the same line, hitting "home" on the
next line takes you to the same character position, except at the beginning of that line.
@param {object} position The character position to set.
@param {boolean} [positionEolStart = true] positionEolStart When rendering the cursor, this state
determines the cursor position when the character position is at a line end: whether to place
the cursor at the start of the next line (positionEolStart = true), or at the end of the
current one (positionEolStart = false). If the cursor position is not at a line end, this state
is ignored by the renderer. Since this state is often "left over" from previous calls to _setPosition
it should not be trusted other than for rendering.
@param {boolean} [resetUpDown = true] resetUpDown Whether to reset the up/down advance
and position values.
|
reset
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
pushChar(c) {
if(!this.attributes) {
this.attributes = c.attributes
}
// push newlines as separate chunks for ease of parsing paragraphs and breaks from chunks
let isNewline = c.char === '\n'
if(isNewline) {
// previous chunk
this.pushChunk()
}
this.chars.push(c.char)
if(isNewline) {
// newline chunk
this.pushChunk()
}
}
|
Sets the character and cursor position within the text. The position is relative to an existing
character given by its replica id. The cursor position is calculated based on the character
position. This is generally straightforward except when the character position is the last
character of a line. In this situation, there are two possible cursor positions: at the end of
the line, or at the beginning of the next line. The desired cursor location depends on how one
got there e.g. hitting "end" on a line should keep you on the same line, hitting "home" on the
next line takes you to the same character position, except at the beginning of that line.
@param {object} position The character position to set.
@param {boolean} [positionEolStart = true] positionEolStart When rendering the cursor, this state
determines the cursor position when the character position is at a line end: whether to place
the cursor at the start of the next line (positionEolStart = true), or at the end of the
current one (positionEolStart = false). If the cursor position is not at a line end, this state
is ignored by the renderer. Since this state is often "left over" from previous calls to _setPosition
it should not be trusted other than for rendering.
@param {boolean} [resetUpDown = true] resetUpDown Whether to reset the up/down advance
and position values.
|
pushChar
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
pushChunk() {
if(this.chars.length > 0) {
contentChunks.push({
text: this.chars.join(''),
attrs: this.attributes
})
}
this.reset()
}
|
Sets the character and cursor position within the text. The position is relative to an existing
character given by its replica id. The cursor position is calculated based on the character
position. This is generally straightforward except when the character position is the last
character of a line. In this situation, there are two possible cursor positions: at the end of
the line, or at the beginning of the next line. The desired cursor location depends on how one
got there e.g. hitting "end" on a line should keep you on the same line, hitting "home" on the
next line takes you to the same character position, except at the beginning of that line.
@param {object} position The character position to set.
@param {boolean} [positionEolStart = true] positionEolStart When rendering the cursor, this state
determines the cursor position when the character position is at a line end: whether to place
the cursor at the start of the next line (positionEolStart = true), or at the end of the
current one (positionEolStart = false). If the cursor position is not at a line end, this state
is ignored by the renderer. Since this state is often "left over" from previous calls to _setPosition
it should not be trusted other than for rendering.
@param {boolean} [resetUpDown = true] resetUpDown Whether to reset the up/down advance
and position values.
|
pushChunk
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
processChar = (c) => {
if (!attributesEqual(currentChunk.attributes, c.attributes)) {
currentChunk.pushChunk()
}
currentChunk.pushChar(c, this)
}
|
Sets the character and cursor position within the text. The position is relative to an existing
character given by its replica id. The cursor position is calculated based on the character
position. This is generally straightforward except when the character position is the last
character of a line. In this situation, there are two possible cursor positions: at the end of
the line, or at the beginning of the next line. The desired cursor location depends on how one
got there e.g. hitting "end" on a line should keep you on the same line, hitting "home" on the
next line takes you to the same character position, except at the beginning of that line.
@param {object} position The character position to set.
@param {boolean} [positionEolStart = true] positionEolStart When rendering the cursor, this state
determines the cursor position when the character position is at a line end: whether to place
the cursor at the start of the next line (positionEolStart = true), or at the end of the
current one (positionEolStart = false). If the cursor position is not at a line end, this state
is ignored by the renderer. Since this state is often "left over" from previous calls to _setPosition
it should not be trusted other than for rendering.
@param {boolean} [resetUpDown = true] resetUpDown Whether to reset the up/down advance
and position values.
|
processChar
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
processChar = (c) => {
if (!attributesEqual(currentChunk.attributes, c.attributes)) {
currentChunk.pushChunk()
}
currentChunk.pushChar(c, this)
}
|
Sets the character and cursor position within the text. The position is relative to an existing
character given by its replica id. The cursor position is calculated based on the character
position. This is generally straightforward except when the character position is the last
character of a line. In this situation, there are two possible cursor positions: at the end of
the line, or at the beginning of the next line. The desired cursor location depends on how one
got there e.g. hitting "end" on a line should keep you on the same line, hitting "home" on the
next line takes you to the same character position, except at the beginning of that line.
@param {object} position The character position to set.
@param {boolean} [positionEolStart = true] positionEolStart When rendering the cursor, this state
determines the cursor position when the character position is at a line end: whether to place
the cursor at the start of the next line (positionEolStart = true), or at the end of the
current one (positionEolStart = false). If the cursor position is not at a line end, this state
is ignored by the renderer. Since this state is often "left over" from previous calls to _setPosition
it should not be trusted other than for rendering.
@param {boolean} [resetUpDown = true] resetUpDown Whether to reset the up/down advance
and position values.
|
processChar
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
_getSelectionRich() {
let selectionChars = this.state.selectionActive ?
this.replica.getTextRange(this.state.selectionLeftChar, this.state.selectionRightChar) :
[]
return this._getContentRich(selectionChars)
}
|
Sets the character and cursor position within the text. The position is relative to an existing
character given by its replica id. The cursor position is calculated based on the character
position. This is generally straightforward except when the character position is the last
character of a line. In this situation, there are two possible cursor positions: at the end of
the line, or at the beginning of the next line. The desired cursor location depends on how one
got there e.g. hitting "end" on a line should keep you on the same line, hitting "home" on the
next line takes you to the same character position, except at the beginning of that line.
@param {object} position The character position to set.
@param {boolean} [positionEolStart = true] positionEolStart When rendering the cursor, this state
determines the cursor position when the character position is at a line end: whether to place
the cursor at the start of the next line (positionEolStart = true), or at the end of the
current one (positionEolStart = false). If the cursor position is not at a line end, this state
is ignored by the renderer. Since this state is often "left over" from previous calls to _setPosition
it should not be trusted other than for rendering.
@param {boolean} [resetUpDown = true] resetUpDown Whether to reset the up/down advance
and position values.
|
_getSelectionRich
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
_modifySelection(toChar, positionEolStart, resetUpDown) {
if(!toChar) return
if(_.isUndefined(resetUpDown)) resetUpDown = true
if(this.cursorMotionTimeout) {
clearTimeout(this.cursorMotionTimeout)
}
let previousSelection = {
selectionActive: this.state.selectionActive,
selectionLeftChar: this.state.selectionLeftChar,
selectionRightChar: this.state.selectionRightChar
}
this.setState((previousState) => {
if(previousState.selectionActive) {
if(charEq(previousState.selectionAnchorChar, previousState.selectionLeftChar)) {
let compareAnchorPos = this.replica.compareCharPos(toChar, previousState.selectionAnchorChar)
if(compareAnchorPos < 0) {
return {
selectionRightChar: previousState.selectionAnchorChar,
selectionLeftChar: toChar,
position: toChar,
positionEolStart: positionEolStart
}
} else if(compareAnchorPos > 0) {
return {
selectionRightChar: toChar,
position: toChar,
positionEolStart: positionEolStart
}
} else {
this._setPosition(previousState.selectionAnchorChar, positionEolStart)
}
} else {
let compareAnchorPos = this.replica.compareCharPos(previousState.selectionAnchorChar, toChar)
if(compareAnchorPos < 0) {
return {
selectionRightChar: toChar,
selectionLeftChar: previousState.selectionAnchorChar,
position: toChar,
positionEolStart: positionEolStart
}
} else if(compareAnchorPos > 0) {
return {
selectionLeftChar: toChar,
position: toChar,
positionEolStart: positionEolStart
}
} else {
this._setPosition(previousState.selectionAnchorChar, positionEolStart)
}
}
} else {
let comparePos = this.replica.compareCharPos(previousState.position, toChar)
if(comparePos === 0) return null
return {
selectionActive: true,
selectionAnchorChar: previousState.position,
selectionLeftChar: comparePos < 0 ? previousState.position : toChar,
selectionRightChar: comparePos > 0 ? previousState.position : toChar,
position: toChar,
positionEolStart: positionEolStart
}
}
// TODO toolbar state based on common rich text attributes of selection
})
if(resetUpDown) {
this.upDownAdvanceX = null
this.upDownPositionEolStart = null
}
let hasSelectionListeners = this.config.eventEmitter.hasListeners('selection-change')
if(previousSelection.selectionActive && !this.state.selectionActive && hasSelectionListeners) {
this.config.eventEmitter.emit('selection-change', null)
} else if (this.state.selectionActive && hasSelectionListeners
&& (previousSelection.selectionActive !== this.state.selectionActive
|| !charEq(previousSelection.selectionLeftChar, this.state.selectionLeftChar)
|| !charEq(previousSelection.selectionRightChar, this.state.selectionRightChar))) {
this.config.eventEmitter.emit('selection-change', { left: this.state.selectionLeftChar, right: this.state.selectionRightChar })
}
this._setRemoteCursorModelState()
}
|
Sets the character and cursor position within the text. The position is relative to an existing
character given by its replica id. The cursor position is calculated based on the character
position. This is generally straightforward except when the character position is the last
character of a line. In this situation, there are two possible cursor positions: at the end of
the line, or at the beginning of the next line. The desired cursor location depends on how one
got there e.g. hitting "end" on a line should keep you on the same line, hitting "home" on the
next line takes you to the same character position, except at the beginning of that line.
@param {object} position The character position to set.
@param {boolean} [positionEolStart = true] positionEolStart When rendering the cursor, this state
determines the cursor position when the character position is at a line end: whether to place
the cursor at the start of the next line (positionEolStart = true), or at the end of the
current one (positionEolStart = false). If the cursor position is not at a line end, this state
is ignored by the renderer. Since this state is often "left over" from previous calls to _setPosition
it should not be trusted other than for rendering.
@param {boolean} [resetUpDown = true] resetUpDown Whether to reset the up/down advance
and position values.
|
_modifySelection
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
_updateSelection(state, onUpdate) {
let possibleSelectionLeftChar = this._relativeChar(state.selectionLeftChar, 0)
let possibleSelectionRightChar = this._relativeChar(state.selectionRightChar, 0)
if(!charEq(state.selectionLeftChar, possibleSelectionLeftChar)
|| !charEq(state.selectionRightChar, possibleSelectionRightChar)) {
let possiblePosition = this._relativeChar(state.position, 0)
let updatedState
if(charEq(possibleSelectionLeftChar, possibleSelectionRightChar)) {
// our entire selection was deleted by a remote
updatedState = {
selectionActive: false,
position: possiblePosition
}
} else {
updatedState = {
selectionLeftChar: possibleSelectionLeftChar,
selectionRightChar: possibleSelectionRightChar,
position: possiblePosition
}
if(state.hasOwnProperty('selectionAnchorChar')) {
updatedState.selectionAnchorChar = this._relativeChar(state.selectionAnchorChar, 0)
}
}
onUpdate(updatedState)
}
}
|
Sets the character and cursor position within the text. The position is relative to an existing
character given by its replica id. The cursor position is calculated based on the character
position. This is generally straightforward except when the character position is the last
character of a line. In this situation, there are two possible cursor positions: at the end of
the line, or at the beginning of the next line. The desired cursor location depends on how one
got there e.g. hitting "end" on a line should keep you on the same line, hitting "home" on the
next line takes you to the same character position, except at the beginning of that line.
@param {object} position The character position to set.
@param {boolean} [positionEolStart = true] positionEolStart When rendering the cursor, this state
determines the cursor position when the character position is at a line end: whether to place
the cursor at the start of the next line (positionEolStart = true), or at the end of the
current one (positionEolStart = false). If the cursor position is not at a line end, this state
is ignored by the renderer. Since this state is often "left over" from previous calls to _setPosition
it should not be trusted other than for rendering.
@param {boolean} [resetUpDown = true] resetUpDown Whether to reset the up/down advance
and position values.
|
_updateSelection
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
_updateRemoteCursorSelectionsLocally() {
Object.values(this.state.remoteCursors).filter(c => c.state.selectionActive).forEach(remoteCursor => {
this._updateSelection(remoteCursor.state, updatedState => {
// we are updating the remote state here directly, a bit messy but we'll get the real update from the remote shortly
let newState= _.merge(_.clone(remoteCursor.state), updatedState)
this.setState(state => {
state.remoteCursors[remoteCursor._id].state = newState
return state
})
})
})
}
|
Sets the character and cursor position within the text. The position is relative to an existing
character given by its replica id. The cursor position is calculated based on the character
position. This is generally straightforward except when the character position is the last
character of a line. In this situation, there are two possible cursor positions: at the end of
the line, or at the beginning of the next line. The desired cursor location depends on how one
got there e.g. hitting "end" on a line should keep you on the same line, hitting "home" on the
next line takes you to the same character position, except at the beginning of that line.
@param {object} position The character position to set.
@param {boolean} [positionEolStart = true] positionEolStart When rendering the cursor, this state
determines the cursor position when the character position is at a line end: whether to place
the cursor at the start of the next line (positionEolStart = true), or at the end of the
current one (positionEolStart = false). If the cursor position is not at a line end, this state
is ignored by the renderer. Since this state is often "left over" from previous calls to _setPosition
it should not be trusted other than for rendering.
@param {boolean} [resetUpDown = true] resetUpDown Whether to reset the up/down advance
and position values.
|
_updateRemoteCursorSelectionsLocally
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
_charPositionRelativeToIndex(charIndex, textChars) {
if(charIndex === 0) {
return this._relativeChar(textChars[0], -1)
} else {
return textChars[charIndex - 1]
}
}
|
Sets the character and cursor position within the text. The position is relative to an existing
character given by its replica id. The cursor position is calculated based on the character
position. This is generally straightforward except when the character position is the last
character of a line. In this situation, there are two possible cursor positions: at the end of
the line, or at the beginning of the next line. The desired cursor location depends on how one
got there e.g. hitting "end" on a line should keep you on the same line, hitting "home" on the
next line takes you to the same character position, except at the beginning of that line.
@param {object} position The character position to set.
@param {boolean} [positionEolStart = true] positionEolStart When rendering the cursor, this state
determines the cursor position when the character position is at a line end: whether to place
the cursor at the start of the next line (positionEolStart = true), or at the end of the
current one (positionEolStart = false). If the cursor position is not at a line end, this state
is ignored by the renderer. Since this state is often "left over" from previous calls to _setPosition
it should not be trusted other than for rendering.
@param {boolean} [resetUpDown = true] resetUpDown Whether to reset the up/down advance
and position values.
|
_charPositionRelativeToIndex
|
javascript
|
ritzyed/ritzy
|
src/flux/EditorStore.js
|
https://github.com/ritzyed/ritzy/blob/master/src/flux/EditorStore.js
|
Apache-2.0
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.