-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create API methods for manipulating with block elements (#135)
* create API methods for manipulating with block elements * get Element implemnted * finish API of elements * add new API object Elements and Blocks * remove elements API from editor.blocks * Publish - @yoopta/[email protected] - @yoopta/[email protected] - @yoopta/[email protected] - @yoopta/[email protected] - @yoopta/[email protected] - @yoopta/[email protected] - @yoopta/[email protected] - @yoopta/[email protected] - @yoopta/[email protected] - @yoopta/[email protected] - @yoopta/[email protected] - @yoopta/[email protected] - @yoopta/[email protected] - @yoopta/[email protected] - @yoopta/[email protected] - @yoopta/[email protected] * add carousel block options
- Loading branch information
1 parent
4c15abd
commit e655c06
Showing
134 changed files
with
3,736 additions
and
1,755 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
12 changes: 11 additions & 1 deletion
12
...itor/src/editor/transforms/deleteBlock.ts → ...e/editor/src/editor/blocks/deleteBlock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { insertBlock } from './insertBlock'; | ||
import { deleteBlock } from './deleteBlock'; | ||
import { moveBlock } from './moveBlock'; | ||
import { focusBlock } from './focusBlock'; | ||
import { splitBlock } from './splitBlock'; | ||
import { increaseBlockDepth } from './increaseBlockDepth'; | ||
import { decreaseBlockDepth } from './decreaseBlockDepth'; | ||
import { duplicateBlock } from './duplicateBlock'; | ||
import { updateBlock } from './updateBlock'; | ||
import { toggleBlock } from './toggleBlock'; | ||
import { insertBlocks } from './insertBlocks'; | ||
|
||
export const Blocks = { | ||
insertBlock, | ||
deleteBlock, | ||
moveBlock, | ||
focusBlock, | ||
splitBlock, | ||
increaseBlockDepth, | ||
decreaseBlockDepth, | ||
duplicateBlock, | ||
updateBlock, | ||
toggleBlock, | ||
insertBlocks, | ||
// [TODO] | ||
// updateBlocks | ||
}; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { Editor, Path, Span, Transforms } from 'slate'; | ||
import { buildBlockElement } from '../../components/Editor/utils'; | ||
import { findSlateBySelectionPath } from '../../utils/findSlateBySelectionPath'; | ||
import { SlateElement, YooEditor } from '../types'; | ||
import { getElementEntry } from './getElementEntry'; | ||
|
||
export type CreateBlockElementOptions = { | ||
path?: 'next' | 'prev' | Path | Span; | ||
focus?: boolean; | ||
split?: boolean; | ||
}; | ||
|
||
export type CreateElement<TElementKeys, TElementProps> = { | ||
type: TElementKeys; | ||
props?: TElementProps; | ||
}; | ||
|
||
export function createElement<TElementKeys extends string, TElementProps>( | ||
editor: YooEditor, | ||
blockId: string, | ||
element: CreateElement<TElementKeys, TElementProps>, | ||
options?: CreateBlockElementOptions, | ||
) { | ||
const blockData = editor.children[blockId]; | ||
if (!blockData) { | ||
throw new Error(`Block with id ${blockId} not found`); | ||
} | ||
|
||
const slate = findSlateBySelectionPath(editor, { at: [blockData.meta.order] }); | ||
if (!slate) { | ||
console.warn('No slate found'); | ||
return; | ||
} | ||
|
||
Editor.withoutNormalizing(slate, () => { | ||
const block = editor.blocks[blockData.type]; | ||
const blockElement = block.elements[element.type]; | ||
const nodeElement = buildBlockElement({ type: element.type, props: { ...blockElement.props, ...element.props } }); | ||
|
||
const elementTypes = Object.keys(block.elements); | ||
|
||
let childrenElements: SlateElement[] = []; | ||
|
||
elementTypes.forEach((blockElementType) => { | ||
const blockElement = block.elements[blockElementType]; | ||
|
||
if (blockElementType === element.type) { | ||
if (Array.isArray(blockElement.children) && blockElement.children.length > 0) { | ||
blockElement.children.forEach((childElementType) => { | ||
const childElement = block.elements[childElementType]; | ||
childrenElements.push(buildBlockElement({ type: childElementType, props: childElement.props })); | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
if (childrenElements.length > 0) nodeElement.children = childrenElements; | ||
|
||
const { path, focus = true } = options || {}; | ||
let atPath; | ||
|
||
const elementEntry = getElementEntry(editor, blockId, { type: element.type }); | ||
|
||
if (elementEntry) { | ||
const [, elementPath] = elementEntry; | ||
|
||
if (Path.isPath(path)) { | ||
atPath = path; | ||
} else if (path === 'prev') { | ||
atPath = Path.previous(elementPath); | ||
} else if (path === 'next') { | ||
atPath = Path.next(elementPath); | ||
} | ||
} | ||
|
||
Transforms.insertNodes(slate, nodeElement, { at: atPath, select: focus }); | ||
|
||
if (focus) { | ||
if (childrenElements.length > 0) { | ||
const firstChild = childrenElements[0]; | ||
const firstElementEntry = getElementEntry(editor, blockId, { | ||
path: atPath, | ||
type: firstChild.type, | ||
}); | ||
|
||
if (firstElementEntry) { | ||
const [, firstElementPath] = firstElementEntry; | ||
Transforms.select(slate, firstElementPath); | ||
} | ||
} | ||
} | ||
|
||
editor.applyChanges(); | ||
editor.emit('change', editor.children); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Editor, Element, Path, Transforms } from 'slate'; | ||
import { findSlateBySelectionPath } from '../../utils/findSlateBySelectionPath'; | ||
import { YooEditor } from '../types'; | ||
|
||
export type DeleteBlockElement = { | ||
type: string; | ||
path: Path; | ||
}; | ||
|
||
export function deleteElement(editor: YooEditor, blockId: string, element: DeleteBlockElement) { | ||
const block = editor.children[blockId]; | ||
|
||
if (!block) { | ||
throw new Error(`Block with id ${blockId} not found`); | ||
} | ||
|
||
const slate = findSlateBySelectionPath(editor, { at: [block.meta.order] }); | ||
|
||
if (!slate) { | ||
console.warn('No slate found'); | ||
return; | ||
} | ||
|
||
Editor.withoutNormalizing(slate, () => { | ||
Transforms.removeNodes(slate, { | ||
at: element.path, | ||
match: (n) => Element.isElement(n) && n.type === element.type, | ||
}); | ||
|
||
editor.applyChanges(); | ||
editor.emit('change', editor.children); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { SlateElement, YooEditor } from '../types'; | ||
import { getElementEntry, GetBlockElementEntryOptions } from './getElementEntry'; | ||
|
||
export type GetBlockElementOptions = GetBlockElementEntryOptions; | ||
|
||
export function getElement<TElementKeys extends string>( | ||
editor: YooEditor, | ||
blockId: string, | ||
options?: GetBlockElementOptions, | ||
): SlateElement<TElementKeys> | undefined { | ||
const elementEntry = getElementEntry(editor, blockId, options); | ||
|
||
if (elementEntry) { | ||
return elementEntry[0] as SlateElement<TElementKeys>; | ||
} | ||
|
||
return undefined; | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/core/editor/src/editor/elements/getElementChildren.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { SlateElement, YooEditor } from '../types'; | ||
import { getElement } from './getElement'; | ||
import { GetBlockElementEntryOptions } from './getElementEntry'; | ||
|
||
export type GetElementChildrenOptions = GetBlockElementEntryOptions; | ||
|
||
export function getElementChildren<TElementKeys extends string>( | ||
editor: YooEditor, | ||
blockId: string, | ||
options?: GetElementChildrenOptions, | ||
): SlateElement<TElementKeys>['children'] | undefined { | ||
const element = getElement(editor, blockId, options); | ||
if (element) return element.children; | ||
|
||
return undefined; | ||
} |
Oops, something went wrong.