generated from KevinBatdorf/gutenberg-rust-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #254 from KevinBatdorf/add-sidebar-slotfil
Add pluggable sidebar slots
- Loading branch information
Showing
6 changed files
with
127 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { createSlotFill } from '@wordpress/components'; | ||
import { useMemo } from '@wordpress/element'; | ||
import { AttributesPropsAndSetter } from '../../types'; | ||
import { decode, encode } from '../../util/code'; | ||
import { languages } from '../../util/languages'; | ||
|
||
export const SlotFactory = ({ | ||
name, | ||
attributes, | ||
setAttributes, | ||
}: AttributesPropsAndSetter & { name: string }) => { | ||
const { Slot } = useMemo(() => { | ||
return createSlotFill(name); | ||
}, [name]); | ||
return ( | ||
<Slot | ||
fillProps={{ | ||
attributes, | ||
setAttributes, | ||
languages, | ||
getCode: () => decode(attributes.code, attributes), | ||
setCode: (code: string) => | ||
setAttributes({ | ||
code: encode(code, attributes), | ||
}), | ||
}} | ||
/> | ||
); | ||
}; |
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,27 @@ | ||
import { escapeHTML } from '@wordpress/escape-html'; | ||
import { decodeEntities } from '@wordpress/html-entities'; | ||
import { Attributes } from '../types'; | ||
|
||
export const encode = (code: string, { useDecodeURI }: Partial<Attributes>) => { | ||
if (useDecodeURI) { | ||
try { | ||
// Here for bw compatability | ||
return encodeURIComponent(code); | ||
} catch (e) { | ||
return code; | ||
} | ||
} | ||
return escapeHTML(code); | ||
}; | ||
|
||
export const decode = (code: string, { useDecodeURI }: Partial<Attributes>) => { | ||
if (useDecodeURI) { | ||
try { | ||
// Here for bw compatability | ||
return decodeURIComponent(code); | ||
} catch (e) { | ||
return code; | ||
} | ||
} | ||
return decodeEntities(code); | ||
}; |