@inscreen/rich-text
Advanced tools
Comparing version 0.1.15 to 0.2.0-rc1
@@ -20,2 +20,3 @@ import { RichTextPosition, RichTextRange } from './selection'; | ||
insertText(position: RichTextPosition, insertion: string): void; | ||
insertParagraphBreak(position: RichTextPosition): void; | ||
insertOpenMention(position: RichTextPosition, triggerCharacter?: string): void; | ||
@@ -22,0 +23,0 @@ closeMention(cursor: RichTextRange, id?: string): void; |
@@ -82,2 +82,13 @@ "use strict"; | ||
} | ||
insertParagraphBreak(position) { | ||
var _a; | ||
const newState = (0, mutations_1.insertParagraphBreak)(this.currentState.text, position); | ||
if (((_a = __classPrivateFieldGet(this, _RichTextEditor_activeCoalescingStatus, "f")) === null || _a === void 0 ? void 0 : _a.type) === 'replacement') { | ||
__classPrivateFieldGet(this, _RichTextEditor_instances, "m", _RichTextEditor_addState).call(this, newState, { coalesce: true }); | ||
__classPrivateFieldSet(this, _RichTextEditor_activeCoalescingStatus, undefined, "f"); | ||
} | ||
else { | ||
__classPrivateFieldGet(this, _RichTextEditor_instances, "m", _RichTextEditor_addState).call(this, newState); | ||
} | ||
} | ||
insertOpenMention(position, triggerCharacter) { | ||
@@ -84,0 +95,0 @@ var _a; |
@@ -7,3 +7,4 @@ import type { RichTextPosition, RichTextRange } from './selection'; | ||
export declare function insertText(text: RichText, position: RichTextPosition, insertion: string): RichTextState; | ||
export declare function insertParagraphBreak(text: RichText, position: RichTextPosition): RichTextState; | ||
export declare function insertOpenMention(text: RichText, previousRange: RichTextRange, position: RichTextPosition): RichTextState; | ||
export declare function closeOpenNode(text: RichText, cursor: RichTextRange, replacement?: RichTextNode): RichTextState; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.closeOpenNode = exports.insertOpenMention = exports.insertText = exports.applyFormat = exports.erase = void 0; | ||
exports.closeOpenNode = exports.insertOpenMention = exports.insertParagraphBreak = exports.insertText = exports.applyFormat = exports.erase = void 0; | ||
const state_1 = require("./state"); | ||
@@ -161,2 +161,27 @@ const runes_1 = require("./utilities/runes"); | ||
exports.insertText = insertText; | ||
function insertParagraphBreak(text, position) { | ||
const range = position.toZeroWidthRange(); | ||
const result = new state_1.RichTextBuilder(range); | ||
let lastSeenParagraphIdx = -1; | ||
(0, state_1.walk)(text, range, (ref) => { | ||
if (ref.paragraphIdx !== lastSeenParagraphIdx) { | ||
lastSeenParagraphIdx = ref.paragraphIdx; | ||
result.addParagraph(); | ||
} | ||
if (ref.matched && ref.node.type === 'text') { | ||
const preText = (0, runes_1.substring)(ref.node.text, 0, position.offset); | ||
const postText = (0, runes_1.substring)(ref.node.text, position.offset); | ||
result.addNode({ ...ref.node, text: preText }); | ||
result.addParagraph(); | ||
result.addNode({ ...ref.node, text: postText }); | ||
result.markRangeStart(0); | ||
result.markRangeEnd(0); | ||
} | ||
else { | ||
result.addNode(ref.node); | ||
} | ||
}); | ||
return result.finalize(); | ||
} | ||
exports.insertParagraphBreak = insertParagraphBreak; | ||
function insertOpenMention(text, previousRange, position) { | ||
@@ -163,0 +188,0 @@ const result = new state_1.RichTextBuilder(previousRange); |
@@ -10,10 +10,21 @@ import type { RichText, RichTextNode } from './types'; | ||
readonly offset: number; | ||
readonly node: RichTextNode; | ||
constructor(text: RichText, nodeId: RichTextNodeId, offset: number); | ||
unsafeMoveToText(text: RichText): RichTextPosition; | ||
get node(): RichTextNode; | ||
nextCharacter(): RichTextPosition; | ||
previousCharacter(): RichTextPosition; | ||
get currentCharacterText(): string | undefined; | ||
get nextCharacter(): RichTextPosition; | ||
get previousCharacter(): RichTextPosition; | ||
get nextCharacterOrNonTextNode(): RichTextPosition; | ||
get previousCharacterOrNonTextNode(): RichTextPosition; | ||
get nextWordBreak(): RichTextPosition; | ||
get previousWordBreak(): RichTextPosition; | ||
get nextParagraphBreak(): RichTextPosition; | ||
get previousParagraphBreak(): RichTextPosition; | ||
equals(other: RichTextPosition): boolean; | ||
toZeroWidthRange(): RichTextRange; | ||
/** Always returns with offset=0 */ | ||
get nextNode(): RichTextPosition | undefined; | ||
/** Always returns with offset=0 */ | ||
get previousNode(): RichTextPosition | undefined; | ||
/** Always returns with offset=0 */ | ||
get nextNodeInParagraph(): RichTextPosition | undefined; | ||
@@ -20,0 +31,0 @@ /** Always returns with offset=0 */ |
@@ -23,2 +23,3 @@ "use strict"; | ||
this.offset = offset; | ||
this.node = __classPrivateFieldGet(this, _RichTextPosition_text, "f").paragraphs[this.nodeId.paragraphIdx].nodes[this.nodeId.nodeIdx]; | ||
} | ||
@@ -28,6 +29,14 @@ unsafeMoveToText(text) { | ||
} | ||
get node() { | ||
return __classPrivateFieldGet(this, _RichTextPosition_text, "f").paragraphs[this.nodeId.paragraphIdx].nodes[this.nodeId.nodeIdx]; | ||
get currentCharacterText() { | ||
if (this.node.type === 'text' || this.node.type === '_open') { | ||
if (this.offset < (0, runes_1.length)(this.node.text)) { | ||
return (0, runes_1.substring)(this.node.text, this.offset, this.offset + 1); | ||
} | ||
const nextCharacterPosition = this.nextCharacterOrNonTextNode; | ||
if (!this.equals(nextCharacterPosition) && nextCharacterPosition.offset > 0) { | ||
return nextCharacterPosition.previousCharacterOrNonTextNode.currentCharacterText; | ||
} | ||
} | ||
} | ||
nextCharacter() { | ||
get nextCharacter() { | ||
if ((this.node.type === 'text' || this.node.type === '_open') && this.offset < (0, runes_1.length)(this.node.text)) { | ||
@@ -37,11 +46,8 @@ return new RichTextPosition(__classPrivateFieldGet(this, _RichTextPosition_text, "f"), this.nodeId, this.offset + 1); | ||
let neighboringNodes = this.node.type === 'text' || this.node.type === '_open'; | ||
for (let paragraphIdx = this.nodeId.paragraphIdx; paragraphIdx < __classPrivateFieldGet(this, _RichTextPosition_text, "f").paragraphs.length; paragraphIdx++) { | ||
const paragraph = __classPrivateFieldGet(this, _RichTextPosition_text, "f").paragraphs[paragraphIdx]; | ||
for (let nodeIdx = paragraphIdx === this.nodeId.paragraphIdx ? this.nodeId.nodeIdx + 1 : 0; nodeIdx < paragraph.nodes.length; nodeIdx++) { | ||
const node = paragraph.nodes[nodeIdx]; | ||
if (node.type === 'text' || node.type === '_open') { | ||
return new RichTextPosition(__classPrivateFieldGet(this, _RichTextPosition_text, "f"), { paragraphIdx, nodeIdx }, neighboringNodes ? 1 : 0); | ||
} | ||
neighboringNodes = false; | ||
for (let pos = this.nextNode; pos !== undefined; pos = pos.nextNode) { | ||
const desiredOffset = neighboringNodes ? 1 : 0; | ||
if ((pos.node.type === 'text' || pos.node.type === '_open') && pos.node.text.length >= desiredOffset) { | ||
return new RichTextPosition(__classPrivateFieldGet(this, _RichTextPosition_text, "f"), pos.nodeId, desiredOffset); | ||
} | ||
neighboringNodes = false; | ||
} | ||
@@ -51,3 +57,3 @@ // End of text | ||
} | ||
previousCharacter() { | ||
get previousCharacter() { | ||
if ((this.node.type === 'text' || this.node.type === '_open') && this.offset > 0) { | ||
@@ -57,12 +63,11 @@ return new RichTextPosition(__classPrivateFieldGet(this, _RichTextPosition_text, "f"), this.nodeId, this.offset - 1); | ||
let neighboringNodes = this.node.type === 'text' || this.node.type === '_open'; | ||
for (let paragraphIdx = this.nodeId.paragraphIdx; paragraphIdx >= 0; paragraphIdx--) { | ||
const paragraph = __classPrivateFieldGet(this, _RichTextPosition_text, "f").paragraphs[paragraphIdx]; | ||
for (let nodeIdx = paragraphIdx === this.nodeId.paragraphIdx ? this.nodeId.nodeIdx - 1 : paragraph.nodes.length - 1; nodeIdx >= 0; nodeIdx--) { | ||
const node = paragraph.nodes[nodeIdx]; | ||
if (node.type === 'text' || node.type === '_open') { | ||
const nodeTextLength = (0, runes_1.length)(node.text); | ||
return new RichTextPosition(__classPrivateFieldGet(this, _RichTextPosition_text, "f"), { paragraphIdx, nodeIdx }, neighboringNodes ? nodeTextLength - 1 : nodeTextLength); | ||
for (let pos = this.nextNode; pos !== undefined; pos = pos.previousNode) { | ||
const desiredNegativeOffset = neighboringNodes ? 1 : 0; | ||
if (pos.node.type === 'text' || pos.node.type === '_open') { | ||
const nodeTextLength = (0, runes_1.length)(pos.node.text); | ||
if (nodeTextLength >= desiredNegativeOffset) { | ||
return new RichTextPosition(__classPrivateFieldGet(this, _RichTextPosition_text, "f"), pos.nodeId, nodeTextLength - desiredNegativeOffset); | ||
} | ||
neighboringNodes = false; | ||
} | ||
neighboringNodes = false; | ||
} | ||
@@ -72,2 +77,135 @@ // Beginning of text | ||
} | ||
get nextCharacterOrNonTextNode() { | ||
if ((this.node.type === 'text' || this.node.type === '_open') && this.offset < (0, runes_1.length)(this.node.text)) { | ||
return new RichTextPosition(__classPrivateFieldGet(this, _RichTextPosition_text, "f"), this.nodeId, this.offset + 1); | ||
} | ||
const neighboringNodes = this.node.type === 'text' || this.node.type === '_open'; | ||
for (let pos = this.nextNode; pos !== undefined; pos = pos.nextNode) { | ||
const desiredOffset = neighboringNodes ? 1 : 0; | ||
if (pos.node.type === 'text' || pos.node.type === '_open') { | ||
if (pos.node.text.length >= desiredOffset) { | ||
return new RichTextPosition(__classPrivateFieldGet(this, _RichTextPosition_text, "f"), pos.nodeId, desiredOffset); | ||
} | ||
} | ||
else { | ||
return pos; | ||
} | ||
} | ||
// End of text | ||
return this; | ||
} | ||
get previousCharacterOrNonTextNode() { | ||
if ((this.node.type === 'text' || this.node.type === '_open') && this.offset > 0) { | ||
return new RichTextPosition(__classPrivateFieldGet(this, _RichTextPosition_text, "f"), this.nodeId, this.offset - 1); | ||
} | ||
const neighboringNodes = this.node.type === 'text' || this.node.type === '_open'; | ||
for (let pos = this.previousNode; pos !== undefined; pos = pos.previousNode) { | ||
const desiredNegativeOffset = neighboringNodes ? 1 : 0; | ||
if (pos.node.type === 'text' || pos.node.type === '_open') { | ||
const nodeTextLength = (0, runes_1.length)(pos.node.text); | ||
if (nodeTextLength >= desiredNegativeOffset) { | ||
return new RichTextPosition(__classPrivateFieldGet(this, _RichTextPosition_text, "f"), pos.nodeId, nodeTextLength - desiredNegativeOffset); | ||
} | ||
} | ||
else { | ||
return pos; | ||
} | ||
} | ||
// Beginning of text | ||
return this; | ||
} | ||
get nextWordBreak() { | ||
if (this.currentCharacterText === undefined) { | ||
return this.nextCharacterOrNonTextNode; | ||
} | ||
const spaceAtStart = /^\p{White_Space}$/u.test(this.currentCharacterText); | ||
// eslint-disable-next-line @typescript-eslint/no-this-alias | ||
let ptr = this; | ||
let ptrCurrentCharacterText = ptr.currentCharacterText; | ||
if (spaceAtStart) { | ||
while (ptrCurrentCharacterText !== undefined && /^\p{White_Space}/u.test(ptrCurrentCharacterText)) { | ||
const nextPtr = ptr.nextCharacterOrNonTextNode; | ||
if (ptr.equals(nextPtr)) { | ||
// End of text | ||
return ptr; | ||
} | ||
ptr = nextPtr; | ||
ptrCurrentCharacterText = ptr.currentCharacterText; | ||
} | ||
} | ||
if (ptrCurrentCharacterText === undefined) { | ||
return ptr.nextCharacterOrNonTextNode; | ||
} | ||
const wordKindRegex = /^\p{Punctuation}$/u.test(ptrCurrentCharacterText) | ||
? /^\p{Punctuation}$/u | ||
: /^\P{Punctuation}$/u; | ||
while (ptrCurrentCharacterText !== undefined && wordKindRegex.test(ptrCurrentCharacterText)) { | ||
const nextPtr = ptr.nextCharacterOrNonTextNode; | ||
if (ptr.equals(nextPtr)) { | ||
// End of text | ||
return ptr; | ||
} | ||
ptr = nextPtr; | ||
ptrCurrentCharacterText = ptr.currentCharacterText; | ||
} | ||
return ptr; | ||
} | ||
get previousWordBreak() { | ||
if (this.currentCharacterText === undefined) { | ||
return this.previousCharacterOrNonTextNode; | ||
} | ||
const spaceAtStart = /^\p{White_Space}$/u.test(this.currentCharacterText); | ||
// eslint-disable-next-line @typescript-eslint/no-this-alias | ||
let ptr = this; | ||
let ptrCurrentCharacterText = ptr.currentCharacterText; | ||
if (spaceAtStart) { | ||
while (ptrCurrentCharacterText !== undefined && /^\p{White_Space}/u.test(ptrCurrentCharacterText)) { | ||
const previousPtr = ptr.previousCharacterOrNonTextNode; | ||
if (ptr.equals(previousPtr)) { | ||
// Beginning of text | ||
return ptr; | ||
} | ||
ptr = previousPtr; | ||
ptrCurrentCharacterText = ptr.currentCharacterText; | ||
} | ||
} | ||
if (ptrCurrentCharacterText === undefined) { | ||
return ptr.previousCharacterOrNonTextNode; | ||
} | ||
const wordKindRegex = /^\p{Punctuation}$/u.test(ptrCurrentCharacterText) | ||
? /^\p{Punctuation}$/u | ||
: /^\P{Punctuation}$/u; | ||
while (ptrCurrentCharacterText !== undefined && wordKindRegex.test(ptrCurrentCharacterText)) { | ||
const previousPtr = ptr.previousCharacterOrNonTextNode; | ||
if (ptr.equals(previousPtr)) { | ||
// Beginning of text | ||
return ptr; | ||
} | ||
ptr = previousPtr; | ||
ptrCurrentCharacterText = ptr.currentCharacterText; | ||
} | ||
return ptr; | ||
} | ||
get nextParagraphBreak() { | ||
const paragraphNodes = __classPrivateFieldGet(this, _RichTextPosition_text, "f").paragraphs[this.nodeId.paragraphIdx].nodes; | ||
const nodeIdx = paragraphNodes.length - 1; | ||
const node = paragraphNodes[nodeIdx]; | ||
const endOfParagraph = new RichTextPosition(__classPrivateFieldGet(this, _RichTextPosition_text, "f"), { ...this.nodeId, nodeIdx }, node.type === 'text' || node.type === '_open' ? node.text.length : 0); | ||
if (this.equals(endOfParagraph) && __classPrivateFieldGet(this, _RichTextPosition_text, "f").paragraphs.length > this.nodeId.paragraphIdx + 1) { | ||
return new RichTextPosition(__classPrivateFieldGet(this, _RichTextPosition_text, "f"), { paragraphIdx: this.nodeId.paragraphIdx + 1, nodeIdx: 0 }, 0) | ||
.nextParagraphBreak; | ||
} | ||
else { | ||
return endOfParagraph; | ||
} | ||
} | ||
get previousParagraphBreak() { | ||
const startOfParagraph = new RichTextPosition(__classPrivateFieldGet(this, _RichTextPosition_text, "f"), { ...this.nodeId, nodeIdx: 0 }, 0); | ||
if (this.equals(startOfParagraph) && this.nodeId.paragraphIdx > 0) { | ||
return new RichTextPosition(__classPrivateFieldGet(this, _RichTextPosition_text, "f"), { paragraphIdx: this.nodeId.paragraphIdx - 1, nodeIdx: 0 }, 0); | ||
} | ||
else { | ||
return startOfParagraph; | ||
} | ||
} | ||
equals(other) { | ||
@@ -83,2 +221,23 @@ return (__classPrivateFieldGet(this, _RichTextPosition_text, "f") === __classPrivateFieldGet(other, _RichTextPosition_text, "f") && | ||
/** Always returns with offset=0 */ | ||
get nextNode() { | ||
const paragraphNodes = __classPrivateFieldGet(this, _RichTextPosition_text, "f").paragraphs[this.nodeId.paragraphIdx].nodes; | ||
if (this.nodeId.nodeIdx < paragraphNodes.length - 1) { | ||
return new RichTextPosition(__classPrivateFieldGet(this, _RichTextPosition_text, "f"), { ...this.nodeId, nodeIdx: this.nodeId.nodeIdx + 1 }, 0); | ||
} | ||
else if (this.nodeId.paragraphIdx < __classPrivateFieldGet(this, _RichTextPosition_text, "f").paragraphs.length - 1) { | ||
return new RichTextPosition(__classPrivateFieldGet(this, _RichTextPosition_text, "f"), { paragraphIdx: this.nodeId.paragraphIdx + 1, nodeIdx: 0 }, 0); | ||
} | ||
} | ||
/** Always returns with offset=0 */ | ||
get previousNode() { | ||
if (this.nodeId.nodeIdx > 0) { | ||
return new RichTextPosition(__classPrivateFieldGet(this, _RichTextPosition_text, "f"), { ...this.nodeId, nodeIdx: this.nodeId.nodeIdx - 1 }, 0); | ||
} | ||
else if (this.nodeId.paragraphIdx > 0) { | ||
const previousParagraphIdx = this.nodeId.paragraphIdx - 1; | ||
const previousParagraph = __classPrivateFieldGet(this, _RichTextPosition_text, "f").paragraphs[previousParagraphIdx]; | ||
return new RichTextPosition(__classPrivateFieldGet(this, _RichTextPosition_text, "f"), { paragraphIdx: previousParagraphIdx, nodeIdx: previousParagraph.nodes.length - 1 }, 0); | ||
} | ||
} | ||
/** Always returns with offset=0 */ | ||
get nextNodeInParagraph() { | ||
@@ -85,0 +244,0 @@ if (__classPrivateFieldGet(this, _RichTextPosition_text, "f").paragraphs[this.nodeId.paragraphIdx].nodes.length > this.nodeId.nodeIdx + 1) { |
{ | ||
"name": "@inscreen/rich-text", | ||
"version": "0.1.15", | ||
"version": "0.2.0-rc1", | ||
"description": "Lightweight rich-text functionality", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
72640
1484