-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Content Model: insertEntity API (#1800)
* Content Model insertEntity * improve * improve * Content Model: Improve cache behavior * fix build * Content Model: improve formatWithContentModel * Content Model: improve formatWithContentModel 2 * Improve * fix build * fix build * improve * add test * add test * add test * add test * fix dark color * fix test * fix build and test
- Loading branch information
1 parent
69005d6
commit a8a9592
Showing
25 changed files
with
3,031 additions
and
27 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
10 changes: 10 additions & 0 deletions
10
demo/scripts/controls/sidePane/contentModelApiPlayground/ApiPaneProps.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,10 @@ | ||
import { IEditor, PluginEvent } from 'roosterjs-editor-types'; | ||
import { SidePaneElementProps } from '../SidePaneElement'; | ||
|
||
export default interface ApiPaneProps extends SidePaneElementProps { | ||
getEditor: () => IEditor; | ||
} | ||
|
||
export interface ApiPlaygroundComponent { | ||
onPluginEvent?: (e: PluginEvent) => void; | ||
} |
4 changes: 4 additions & 0 deletions
4
demo/scripts/controls/sidePane/contentModelApiPlayground/ApiPlaygroundPane.scss
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,4 @@ | ||
.header { | ||
flex: 0 0 auto; | ||
padding-bottom: 5px; | ||
} |
69 changes: 69 additions & 0 deletions
69
demo/scripts/controls/sidePane/contentModelApiPlayground/ApiPlaygroundPane.tsx
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,69 @@ | ||
import * as React from 'react'; | ||
import apiEntries, { ApiPlaygroundReactComponent } from './apiEntries'; | ||
import ApiPaneProps from './ApiPaneProps'; | ||
import { getObjectKeys } from 'roosterjs-editor-dom'; | ||
import { PluginEvent } from 'roosterjs-editor-types'; | ||
import { SidePaneElement } from '../SidePaneElement'; | ||
|
||
const styles = require('./ApiPlaygroundPane.scss'); | ||
|
||
export interface ApiPlaygroundPaneState { | ||
current: string; | ||
} | ||
|
||
export default class ApiPlaygroundPane extends React.Component<ApiPaneProps, ApiPlaygroundPaneState> | ||
implements SidePaneElement { | ||
private select = React.createRef<HTMLSelectElement>(); | ||
private pane = React.createRef<ApiPlaygroundReactComponent>(); | ||
constructor(props: ApiPaneProps) { | ||
super(props); | ||
this.state = { current: 'empty' }; | ||
} | ||
|
||
render() { | ||
let componentClass = apiEntries[this.state.current].component; | ||
let pane: JSX.Element = null; | ||
if (componentClass) { | ||
pane = React.createElement(componentClass, { ...this.props, ref: this.pane }); | ||
} | ||
|
||
return ( | ||
<> | ||
<div className={styles.header}> | ||
<h3>Select an API to try</h3> | ||
|
||
<select ref={this.select} value={this.state.current} onChange={this.onChange}> | ||
{getObjectKeys(apiEntries).map(key => ( | ||
<option value={key} key={key}> | ||
{apiEntries[key].name} | ||
</option> | ||
))} | ||
</select> | ||
</div> | ||
{pane} | ||
</> | ||
); | ||
} | ||
|
||
onPluginEvent(e: PluginEvent) { | ||
if (this.pane.current && this.pane.current.onPluginEvent) { | ||
this.pane.current.onPluginEvent(e); | ||
} | ||
} | ||
|
||
setHashPath(path: string[]) { | ||
let paneName = path && getObjectKeys(apiEntries).indexOf(path[0]) >= 0 ? path[0] : null; | ||
|
||
if (paneName && paneName != this.state.current) { | ||
this.setState({ | ||
current: paneName, | ||
}); | ||
} else { | ||
this.props.updateHash(null, [this.state.current]); | ||
} | ||
} | ||
|
||
private onChange = () => { | ||
this.props.updateHash(null, [this.select.current.value]); | ||
}; | ||
} |
27 changes: 27 additions & 0 deletions
27
demo/scripts/controls/sidePane/contentModelApiPlayground/ApiPlaygroundPlugin.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,27 @@ | ||
import ApiPaneProps from './ApiPaneProps'; | ||
import ApiPlaygroundPane from './ApiPlaygroundPane'; | ||
import SidePanePluginImpl from '../SidePanePluginImpl'; | ||
import { PluginEvent } from 'roosterjs-editor-types'; | ||
import { SidePaneElementProps } from '../SidePaneElement'; | ||
|
||
export default class ApiPlaygroundPlugin extends SidePanePluginImpl< | ||
ApiPlaygroundPane, | ||
ApiPaneProps | ||
> { | ||
constructor() { | ||
super(ApiPlaygroundPane, 'api', 'API Playground'); | ||
} | ||
|
||
getComponentProps(base: SidePaneElementProps) { | ||
return { | ||
...base, | ||
getEditor: () => { | ||
return this.editor; | ||
}, | ||
}; | ||
} | ||
|
||
onPluginEvent(e: PluginEvent) { | ||
this.getComponent(component => component.onPluginEvent(e)); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
demo/scripts/controls/sidePane/contentModelApiPlayground/apiEntries.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,27 @@ | ||
import * as React from 'react'; | ||
import ApiPaneProps, { ApiPlaygroundComponent } from './ApiPaneProps'; | ||
import InsertEntityPane from './insertEntity/InsertEntityPane'; | ||
|
||
export interface ApiPlaygroundReactComponent | ||
extends React.Component<ApiPaneProps, any>, | ||
ApiPlaygroundComponent {} | ||
|
||
interface ApiEntry { | ||
name: string; | ||
component?: { new (prpos: ApiPaneProps): ApiPlaygroundReactComponent }; | ||
} | ||
|
||
const apiEntries: { [key: string]: ApiEntry } = { | ||
empty: { | ||
name: 'Please select', | ||
}, | ||
entity: { | ||
name: 'Insert Entity', | ||
component: InsertEntityPane, | ||
}, | ||
more: { | ||
name: 'Coming soon...', | ||
}, | ||
}; | ||
|
||
export default apiEntries; |
6 changes: 6 additions & 0 deletions
6
demo/scripts/controls/sidePane/contentModelApiPlayground/insertEntity/InsertEntityPane.scss
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,6 @@ | ||
.textarea { | ||
outline: none; | ||
resize: none; | ||
min-height: 40px; | ||
width: 90%; | ||
} |
177 changes: 177 additions & 0 deletions
177
demo/scripts/controls/sidePane/contentModelApiPlayground/insertEntity/InsertEntityPane.tsx
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,177 @@ | ||
import * as React from 'react'; | ||
import ApiPaneProps from '../ApiPaneProps'; | ||
import { Entity } from 'roosterjs-editor-types'; | ||
import { getEntityFromElement, getEntitySelector } from 'roosterjs-editor-dom'; | ||
import { trustedHTMLHandler } from '../../../../utils/trustedHTMLHandler'; | ||
import { | ||
IContentModelEditor, | ||
insertEntity, | ||
InsertEntityOptions, | ||
} from 'roosterjs-content-model-editor'; | ||
|
||
const styles = require('./InsertEntityPane.scss'); | ||
|
||
interface InsertEntityPaneState { | ||
entities: Entity[]; | ||
} | ||
|
||
export default class InsertEntityPane extends React.Component<ApiPaneProps, InsertEntityPaneState> { | ||
private entityType = React.createRef<HTMLInputElement>(); | ||
private html = React.createRef<HTMLTextAreaElement>(); | ||
private styleInline = React.createRef<HTMLInputElement>(); | ||
private styleBlock = React.createRef<HTMLInputElement>(); | ||
private focusAfterEntity = React.createRef<HTMLInputElement>(); | ||
|
||
private posFocus = React.createRef<HTMLInputElement>(); | ||
private posTop = React.createRef<HTMLInputElement>(); | ||
private posBottom = React.createRef<HTMLInputElement>(); | ||
private posRegionRoot = React.createRef<HTMLInputElement>(); | ||
|
||
constructor(props: ApiPaneProps) { | ||
super(props); | ||
this.state = { | ||
entities: [], | ||
}; | ||
} | ||
|
||
render() { | ||
return ( | ||
<> | ||
<div> | ||
Type: <input type="input" ref={this.entityType} /> | ||
</div> | ||
<div> | ||
HTML: <textarea className={styles.textarea} ref={this.html}></textarea> | ||
</div> | ||
<div> | ||
Style: | ||
<input | ||
type="radio" | ||
name="entityStyle" | ||
ref={this.styleInline} | ||
id="styleInline" | ||
/> | ||
<label htmlFor="styleInline">Inline</label> | ||
<input type="radio" name="entityStyle" ref={this.styleBlock} id="styleBlock" /> | ||
<label htmlFor="styleBlock">Block</label> | ||
</div> | ||
<div> | ||
Position: | ||
<br /> | ||
<input type="radio" name="position" ref={this.posFocus} id="posFocus" /> | ||
<label htmlFor="posFocus">Current focus</label> | ||
<br /> | ||
<input type="radio" name="position" ref={this.posTop} id="posTop" /> | ||
<label htmlFor="posTop">Top</label> | ||
<br /> | ||
<input type="radio" name="position" ref={this.posBottom} id="posBottom" /> | ||
<label htmlFor="posBottom">Bottom</label> | ||
<br /> | ||
<input | ||
type="radio" | ||
name="position" | ||
ref={this.posRegionRoot} | ||
id="posRegionRoot" | ||
/> | ||
<label htmlFor="posRegionRoot">Region root</label> | ||
<br /> | ||
</div> | ||
<div> | ||
<input id="focusAfterEntity" type="checkbox" ref={this.focusAfterEntity} /> | ||
<label htmlFor="focusAfterEntity">Focus after entity</label> | ||
</div> | ||
<div> | ||
<button onClick={this.insertEntity}>Insert Entity</button> | ||
</div> | ||
<hr /> | ||
<div> | ||
<button onClick={this.onGetEntities}>Get all entities</button> | ||
</div> | ||
<div> | ||
{this.state.entities.map(entity => ( | ||
<EntityButton key={entity.id} entity={entity} /> | ||
))} | ||
</div> | ||
</> | ||
); | ||
} | ||
|
||
private insertEntity = () => { | ||
const entityType = this.entityType.current.value; | ||
const node = document.createElement('span'); | ||
node.innerHTML = trustedHTMLHandler(this.html.current.value); | ||
const isBlock = this.styleBlock.current.checked; | ||
const focusAfterEntity = this.focusAfterEntity.current.checked; | ||
const insertAtTop = this.posTop.current.checked; | ||
const insertAtBottom = this.posBottom.current.checked; | ||
const insertAtRoot = this.posRegionRoot.current.checked; | ||
|
||
if (node) { | ||
const editor = this.props.getEditor(); | ||
|
||
editor.addUndoSnapshot(() => { | ||
const options: InsertEntityOptions = { | ||
contentNode: node, | ||
focusAfterEntity: focusAfterEntity, | ||
}; | ||
|
||
if (isBlock) { | ||
insertEntity( | ||
editor as IContentModelEditor, | ||
entityType, | ||
true, | ||
insertAtRoot | ||
? 'root' | ||
: insertAtTop | ||
? 'begin' | ||
: insertAtBottom | ||
? 'end' | ||
: 'focus', | ||
options | ||
); | ||
} else { | ||
insertEntity( | ||
editor as IContentModelEditor, | ||
entityType, | ||
isBlock, | ||
insertAtTop ? 'begin' : insertAtBottom ? 'end' : 'focus', | ||
options | ||
); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
private onGetEntities = () => { | ||
const selector = getEntitySelector(); | ||
const nodes = this.props.getEditor().queryElements(selector); | ||
const allEntities = nodes.map(node => getEntityFromElement(node)); | ||
|
||
this.setState({ | ||
entities: allEntities.filter(e => !!e), | ||
}); | ||
}; | ||
} | ||
|
||
function EntityButton({ entity }: { entity: Entity }) { | ||
let background = ''; | ||
const onMouseOver = React.useCallback(() => { | ||
background = entity.wrapper.style.backgroundColor; | ||
entity.wrapper.style.backgroundColor = 'blue'; | ||
}, [entity]); | ||
|
||
const onMouseOut = React.useCallback(() => { | ||
entity.wrapper.style.backgroundColor = background; | ||
}, [entity]); | ||
|
||
return ( | ||
<div onMouseOver={onMouseOver} onMouseOut={onMouseOut}> | ||
Type: {entity.type} | ||
<br /> | ||
Id: {entity.id} | ||
<br /> | ||
Readonly: {entity.isReadonly ? 'True' : 'False'} | ||
<br /> | ||
</div> | ||
); | ||
} |
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
Oops, something went wrong.