Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
//
// insert
//
RT.insert(VALUE, VALUE);
RT.insert(VALUE, VALUE, 10);
RT.insert(VALUE, VALUE, 10, 20);
//
// insertObject
//
RT.insertObject(VALUE, FORMAT);
//
// isCollapsed
//
RT.isCollapsed(VALUE);
//
// isEmpty
//
RT.isEmpty(VALUE);
//
// join
//
RT.join([VALUE, VALUE]);
RT.join([], VALUE);
RT.join([], 'foo');
RT.join([VALUE], 'foo');
//
// registerFormatType
const addMarker = ( value, data ) => {
const id = nanoid( 8 );
const item = { ...data, id };
const format = {
type: 'ol/attributions',
attributes: {
href: `#ref-${ id }`,
id: `anchor-${ id }`,
},
};
// Add attribution.
dispatch( 'openlab/attributions' ).add( item );
const startIndex = isCollapsed( value ) ? value.start : value.end;
const newValue = insertObject( value, format, startIndex );
return newValue;
};
const addFootnote = ( value ) => {
// It does not matter what this is, as long as it is unique per page.
const id = uuid();
const format = {
type: name,
attributes: {
href: `#${ id }`,
id: `${ id }-anchor`,
'data-core-footnotes-id': id,
},
};
let newValue;
if ( isCollapsed( value ) ) {
const prevStart = value.start;
newValue = insertObject( value, format );
newValue.start = prevStart;
} else {
newValue = applyFormat( value, format );
}
return newValue;
};
getLinkSelection() {
const { value, isActive } = this.props;
const startFormat = getActiveFormat( value, 'core/link' );
// if the link isn't selected, get the link manually by looking around the cursor
// TODO: handle partly selected links
if ( startFormat && isCollapsed( value ) && isActive ) {
let startIndex = value.start;
let endIndex = value.end;
while ( find( value.formats[ startIndex ], startFormat ) ) {
startIndex--;
}
endIndex++;
while ( find( value.formats[ endIndex ], startFormat ) ) {
endIndex++;
}
return {
...value,
start: startIndex + 1,
submitLink() {
const { isActive, onChange, speak, value } = this.props;
const { inputValue, opensInNewWindow, text } = this.state;
const url = prependHTTP( inputValue );
const linkText = text || inputValue;
const format = createLinkFormat( {
url,
opensInNewWindow,
text: linkText,
} );
if ( isCollapsed( value ) && ! isActive ) { // insert link
const toInsert = applyFormat( create( { text: linkText } ), format, 0, linkText.length );
const newAttributes = insert( value, toInsert );
onChange( { ...newAttributes, needsSelectionUpdate: true } );
} else if ( text !== getTextContent( slice( value ) ) ) { // edit text in selected link
const toInsert = applyFormat( create( { text } ), format, 0, text.length );
const newAttributes = insert( value, toInsert, value.start, value.end );
onChange( { ...newAttributes, needsSelectionUpdate: true } );
} else { // transform selected text into link
const newAttributes = applyFormat( value, format );
onChange( { ...newAttributes, needsSelectionUpdate: true } );
}
if ( ! isValidHref( url ) ) {
speak( __( 'Warning: the link has been inserted but may have errors. Please test it.' ), 'assertive' );
} else if ( isActive ) {
speak( __( 'Link edited.' ), 'assertive' );
componentDidUpdate( prevProps, prevState ) {
const { record, completers } = this.props;
const { record: prevRecord } = prevProps;
const { open: prevOpen } = prevState;
if ( ( ! this.state.open ) !== ( ! prevOpen ) ) {
this.toggleKeyEvents( ! ! this.state.open );
}
if ( isCollapsed( record ) ) {
const text = deburr( getTextContent( slice( record, 0 ) ) );
const prevText = deburr( getTextContent( slice( prevRecord, 0 ) ) );
if ( text !== prevText ) {
const textAfterSelection = getTextContent( slice( record, undefined, getTextContent( record ).length ) );
const allCompleters = map( completers, ( completer, idx ) => ( { ...completer, idx } ) );
const open = find( allCompleters, ( { triggerPrefix, allowContext } ) => {
const index = text.lastIndexOf( triggerPrefix );
if ( index === -1 ) {
return false;
}
if ( allowContext && ! allowContext( text.slice( 0, index ), textAfterSelection ) ) {
return false;
}
submitLink( event ) {
const { isActive, value, onChange, speak } = this.props;
const { inputValue, opensInNewWindow } = this.state;
const url = prependHTTP( inputValue );
const selectedText = getTextContent( slice( value ) );
const format = createLinkFormat( {
url,
opensInNewWindow,
text: selectedText,
} );
event.preventDefault();
if ( isCollapsed( value ) && ! isActive ) {
const toInsert = applyFormat( create( { text: url } ), format, 0, url.length );
onChange( insert( value, toInsert ) );
} else {
onChange( applyFormat( value, format ) );
}
this.resetState();
if ( ! isValidHref( url ) ) {
speak( __( 'Warning: the link has been inserted but may have errors. Please test it.' ), 'assertive' );
} else if ( isActive ) {
speak( __( 'Link edited.' ), 'assertive' );
} else {
speak( __( 'Link inserted.' ), 'assertive' );
}
}