🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

react-dialogic

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-dialogic - npm Package Compare versions

Comparing version

to
1.4.3

2

dist/index.d.ts

@@ -18,3 +18,3 @@ import React from 'react';

*/
next: string;
next?: string;
}

@@ -21,0 +21,0 @@ /**

@@ -1,2 +0,2 @@

import{jsx as e,jsxs as n,Fragment as t}from"react/jsx-runtime";import{createContext as r,useState as o,useCallback as a,useContext as c,useMemo as i,useEffect as l}from"react";var u=function(){return u=Object.assign||function(e){for(var n,t=1,r=arguments.length;t<r;t++)for(var o in n=arguments[t])Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o]);return e},u.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var s=r({currentScene:null,currentIndex:0,dialogue:{},characters:{},setCurrentScene:function(){},handleNext:function(){},handleChoiceSelect:function(){},getCurrentItem:function(){return null},getCurrentScene:function(){return null},handleTypingComplete:function(){}}),d=function(n){var t=n.children,r=n.dialogue,c=n.characters,i=n.startScene,l=n.onMessageStart,d=n.onMessage,g=n.onMessageEnd,m=o(i||null),h=m[0],f=m[1],v=o(0),p=v[0],x=v[1],_=o(!1),C=_[0],y=_[1],S=a((function(){return h&&r[h]?r[h]:null}),[h,r]),N=a((function(){var e=S();return!e||!e.sequence||p>=e.sequence.length?null:e.sequence[p]}),[S,p]),b=a((function(e,n){f(e),x(n),y(!1);var t=r[e];t&&t.sequence&&t.sequence[n]&&l&&l(t.sequence[n])}),[r,l]),M=a((function(){y(!0);var e=N();e&&d&&d(e)}),[N,d]),k=a((function(){var e=S();if(e){var n=N();n&&(C?n.choices||(p<e.sequence.length-1?b(h,p+1):(g&&g(n),n.next&&b(n.next,0))):M())}}),[h,p,S,N,C,M,b,g]),T=a((function(e){e?b(e,0):b(h,p+1)}),[b]);return e(s.Provider,u({value:{currentScene:h,currentIndex:p,dialogue:r,characters:c,setCurrentScene:function(e){return b(e,0)},handleNext:k,handleChoiceSelect:T,getCurrentItem:N,getCurrentScene:S,onMessageStart:l,onMessage:d,onMessageEnd:g,handleTypingComplete:M}},{children:t}))},g=function(n){var t=n.children,r=n.characters,o=n.dialogue,a=n.startScene,c=n.onMessageStart,i=n.onMessage,l=n.onMessageEnd;return e(d,u({characters:r,dialogue:o,startScene:a,onMessageStart:c,onMessage:i,onMessageEnd:l},{children:t}))},m="Character-module_character__WMx3-",h="Character-module_characterEnter__x7Q7O",f="Character-module_characterExit__yGDp3",v=function(n){var t=n.config,r=n.emotion,o=void 0===r?"default":r,a=n.position,c=void 0===a?"center":a,l=n.active,s=void 0===l||l,d=i((function(){return t.images[o]||t.images.default}),[t.images,o]),g=i((function(){switch(c){case"left":return{left:"var(--dialogic-character-side-margin, 1rem)",right:"auto",transform:"translateX(0)"};case"right":return{left:"auto",right:"var(--dialogic-character-side-margin, 1rem)",transform:"translateX(0)"};default:return{left:"50%",right:"auto",transform:"translateX(-50%)",bottom:"var(--dialogic-character-bottom, 0)"}}}),[c]),v=s?"opacity-100":"opacity-50",p=s?h:f;return e("div",u({className:"".concat(m," ").concat(v," ").concat(p," pointer-events-auto"),style:u({position:"absolute",height:"center"===c?"auto":"100%",display:"flex",alignItems:"flex-end"},g)},{children:e("img",{src:d,alt:t.name,className:"center"===c?"w-full h-auto object-contain":void 0})}))};var p="DialogBox-module_dialogBox__--CNd",x="DialogBox-module_characterName__6UKbn",_="DialogBox-module_dialogText__APDO3",C="DialogBox-module_continueIndicator__ZJppK",y=function(t){var r=t.name,c=t.text,i=t.textColor,s=t.isTyping,d=void 0===s||s,g=t.typingSpeed,m=t.onTypingComplete,h=t.onNext,f=function(e,n){void 0===n&&(n={});var t=n.speed,r=void 0===t?30:t,c=n.startTyping,i=void 0===c||c,u=n.onComplete,s=n.punctuationDelay,d=void 0===s?{",":150,".":300,"!":300,"?":300,";":200,":":200}:s,g=o(""),m=g[0],h=g[1],f=o(!1),v=f[0],p=f[1],x=o(0),_=x[0],C=x[1],y=a((function(){h(e),C(e.length),p(!0),null==u||u()}),[e,u]);l((function(){if(i&&!v){if(0===e.length)return p(!0),void(null==u||u());if(_>=e.length)return console.log("currentIndex >= text.length"),p(!0),void(null==u||u());var n=e[_],t=d[n]||r,o=setTimeout((function(){h(e.substring(0,_+1)),C((function(e){return e+1}))}),t);return function(){return clearTimeout(o)}}}),[e,_,r,i,v,u,d]);var S=a((function(){h(""),C(0),p(!1)}),[]);return l((function(){S()}),[e,S]),{displayText:m,isComplete:v,complete:y,reset:S}}(c,{speed:g||40,startTyping:d,onComplete:m}),v=f.displayText,y=f.isComplete,S=f.complete;l((function(){d||S()}),[d,S]);var N=function(){y?null==h||h():S()};return n("div",u({className:p,onClick:N,onKeyDown:function(e){"Enter"!==e.key&&" "!==e.key||N()},tabIndex:0,role:"button","aria-label":"繼續對話"},{children:[r&&e("div",u({className:x,style:i?{color:i}:void 0},{children:r})),e("div",u({className:_},{children:v})),y&&e("div",u({className:C},{children:"▼"}))]}))},S="ChoiceMenu-module_choices__uaEql",N="ChoiceMenu-module_choiceItem__4I3Fk",b=function(t){var r=t.choices,o=t.onSelect;return e("div",u({className:S},{children:r.map((function(t,r){return n("button",u({className:N,onClick:function(){return o(t.next)}},{children:[t.icon&&e("span",u({className:"mr-2"},{children:t.icon})),e("span",{children:t.text})]}),"choice-".concat(r))}))}))},M="DialogicContainer-module_container__excEe",k="DialogicContainer-module_charactersContainer__JyNka",T="DialogicContainer-module_dialogArea__0hEYq",D="DialogicContainer-module_background__KDi75",E=function(r){var a=r.src,c=r.color,i=void 0===c?"transparent":c,s=r.transitionDuration,d=void 0===s?500:s,g=o(a),m=g[0],h=g[1],f=o(void 0),v=f[0],p=f[1],x=o(!1),_=x[0],C=x[1];l((function(){if(a!==m){p(m),h(a),C(!0);var e=setTimeout((function(){C(!1),p(void 0)}),d);return function(){return clearTimeout(e)}}}),[a,m,d]);var y=function(e){return{position:"absolute",inset:0,backgroundImage:e?"url(".concat(e,")"):"none",backgroundColor:e?void 0:i,backgroundSize:"cover",backgroundPosition:"center",transition:"opacity ".concat(d,"ms ease-in-out"),zIndex:-1}};return n(t,{children:[e("div",{className:D,style:u(u({},y(m)),{opacity:1})}),_&&v&&e("div",{className:D,style:u(u({},y(v)),{opacity:0})})]})},I=function(){var t=c(s),r=t.getCurrentItem,o=t.getCurrentScene,a=t.handleNext,i=t.handleChoiceSelect,l=t.characters,d=t.handleTypingComplete,g=o(),m=r();if(!g||!m)return null;var h=m.background||g.background,f=m.speaker?l[m.speaker]:null;return n("div",u({className:"".concat(M," flex flex-col"),style:{userSelect:"none"}},{children:[e(E,{src:h}),e("div",u({className:k},{children:f&&e(v,{config:f,emotion:m.emotion,position:m.position||f.defaultPosition,active:!0})})),e("div",u({className:T},{children:n("div",u({className:"w-full max-w-5xl mx-auto px-4 mb-4"},{children:[e(y,{name:null==f?void 0:f.name,text:m.text,textColor:null==f?void 0:f.textColor,onNext:function(){a()},onTypingComplete:d}),m.choices&&e(b,{choices:m.choices,onSelect:i})]}))}))]}))},q=function(n){var t=n.characters,r=n.dialogue,o=n.startScene,a=n.onMessageStart,c=n.onMessage,i=n.onMessageEnd;return e(g,u({characters:t,dialogue:r,startScene:o,onMessageStart:a,onMessage:c,onMessageEnd:i},{children:e(I,{})}))};export{q as ReactDialogic};
import{jsx as e,jsxs as n,Fragment as t}from"react/jsx-runtime";import{createContext as r,useState as o,useCallback as a,useContext as i,useMemo as c,useEffect as l}from"react";var u=function(){return u=Object.assign||function(e){for(var n,t=1,r=arguments.length;t<r;t++)for(var o in n=arguments[t])Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o]);return e},u.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var s=r({currentScene:null,currentIndex:0,dialogue:{},characters:{},setCurrentScene:function(){},handleNext:function(){},handleChoiceSelect:function(){},getCurrentItem:function(){return null},getCurrentScene:function(){return null},handleTypingComplete:function(){}}),d=function(n){var t=n.children,r=n.dialogue,i=n.characters,c=n.startScene,l=n.onMessageStart,d=n.onMessage,g=n.onMessageEnd,m=o(c||null),h=m[0],f=m[1],v=o(0),p=v[0],x=v[1],_=o(!1),C=_[0],y=_[1],S=a((function(){return h&&r[h]?r[h]:null}),[h,r]),N=a((function(){var e=S();return!e||!e.sequence||p>=e.sequence.length?null:e.sequence[p]}),[S,p]),b=a((function(e,n){f(e),x(n),y(!1);var t=r[e];t&&t.sequence&&t.sequence[n]&&l&&l(t.sequence[n])}),[r,l]),M=a((function(){y(!0);var e=N();e&&d&&d(e)}),[N,d]),k=a((function(){var e=S();if(e){var n=N();n&&(C?n.choices||(p<e.sequence.length-1?b(h,p+1):(g&&g(n),n.next&&b(n.next,0))):M())}}),[h,p,S,N,C,M,b,g]),T=a((function(e){e?b(e,0):b(h,p+1)}),[b,h,p]);return e(s.Provider,u({value:{currentScene:h,currentIndex:p,dialogue:r,characters:i,setCurrentScene:function(e){return b(e,0)},handleNext:k,handleChoiceSelect:T,getCurrentItem:N,getCurrentScene:S,onMessageStart:l,onMessage:d,onMessageEnd:g,handleTypingComplete:M}},{children:t}))},g=function(n){var t=n.children,r=n.characters,o=n.dialogue,a=n.startScene,i=n.onMessageStart,c=n.onMessage,l=n.onMessageEnd;return e(d,u({characters:r,dialogue:o,startScene:a,onMessageStart:i,onMessage:c,onMessageEnd:l},{children:t}))},m="Character-module_character__WMx3-",h="Character-module_characterEnter__x7Q7O",f="Character-module_characterExit__yGDp3",v=function(n){var t=n.config,r=n.emotion,o=void 0===r?"default":r,a=n.position,i=void 0===a?"center":a,l=n.active,s=void 0===l||l,d=c((function(){return t.images[o]||t.images.default}),[t.images,o]),g=c((function(){switch(i){case"left":return{left:"var(--dialogic-character-side-margin, 1rem)",right:"auto",transform:"translateX(0)"};case"right":return{left:"auto",right:"var(--dialogic-character-side-margin, 1rem)",transform:"translateX(0)"};default:return{left:"50%",right:"auto",transform:"translateX(-50%)",bottom:"var(--dialogic-character-bottom, 0)"}}}),[i]),v=s?"opacity-100":"opacity-50",p=s?h:f;return e("div",u({className:"".concat(m," ").concat(v," ").concat(p," pointer-events-auto"),style:u({position:"absolute",height:"center"===i?"auto":"100%",display:"flex",alignItems:"flex-end"},g)},{children:e("img",{src:d,alt:t.name,className:"center"===i?"w-full h-auto object-contain":void 0})}))};var p="DialogBox-module_dialogBox__--CNd",x="DialogBox-module_characterName__6UKbn",_="DialogBox-module_dialogText__APDO3",C="DialogBox-module_continueIndicator__ZJppK",y=function(t){var r=t.name,i=t.text,c=t.textColor,s=t.isTyping,d=void 0===s||s,g=t.typingSpeed,m=t.onTypingComplete,h=t.onNext,f=function(e,n){void 0===n&&(n={});var t=n.speed,r=void 0===t?30:t,i=n.startTyping,c=void 0===i||i,u=n.onComplete,s=n.punctuationDelay,d=void 0===s?{",":150,".":300,"!":300,"?":300,";":200,":":200}:s,g=o(""),m=g[0],h=g[1],f=o(!1),v=f[0],p=f[1],x=o(0),_=x[0],C=x[1],y=a((function(){h(e),C(e.length),p(!0),null==u||u()}),[e,u]);l((function(){if(c&&!v){if(0===e.length)return p(!0),void(null==u||u());if(_>=e.length)return p(!0),void(null==u||u());var n=e[_],t=d[n]||r,o=setTimeout((function(){h(e.substring(0,_+1)),C((function(e){return e+1}))}),t);return function(){return clearTimeout(o)}}}),[e,_,r,c,v,u,d]);var S=a((function(){h(""),C(0),p(!1)}),[]);return l((function(){S()}),[e,S]),{displayText:m,isComplete:v,complete:y,reset:S}}(i,{speed:g||40,startTyping:d,onComplete:m}),v=f.displayText,y=f.isComplete,S=f.complete;l((function(){d||S()}),[d,S]);var N=function(){y?null==h||h():S()};return n("div",u({className:p,onClick:N,onKeyDown:function(e){"Enter"!==e.key&&" "!==e.key||N()},tabIndex:0,role:"button","aria-label":"繼續對話"},{children:[r&&e("div",u({className:x,style:c?{color:c}:void 0},{children:r})),e("div",u({className:_},{children:v})),y&&e("div",u({className:C},{children:"▼"}))]}))},S="ChoiceMenu-module_choices__uaEql",N="ChoiceMenu-module_choiceItem__4I3Fk",b=function(t){var r=t.choices,o=t.onSelect;return e("div",u({className:S},{children:r.map((function(t,r){return n("button",u({className:N,onClick:function(){return o(t.next)}},{children:[t.icon&&e("span",u({className:"mr-2"},{children:t.icon})),e("span",{children:t.text})]}),"choice-".concat(r))}))}))},M="DialogicContainer-module_container__excEe",k="DialogicContainer-module_charactersContainer__JyNka",T="DialogicContainer-module_dialogArea__0hEYq",D="DialogicContainer-module_background__KDi75",E=function(r){var a=r.src,i=r.color,c=void 0===i?"transparent":i,s=r.transitionDuration,d=void 0===s?500:s,g=o(a),m=g[0],h=g[1],f=o(void 0),v=f[0],p=f[1],x=o(!1),_=x[0],C=x[1];l((function(){if(a!==m){p(m),h(a),C(!0);var e=setTimeout((function(){C(!1),p(void 0)}),d);return function(){return clearTimeout(e)}}}),[a,m,d]);var y=function(e){return{position:"absolute",inset:0,backgroundImage:e?"url(".concat(e,")"):"none",backgroundColor:e?void 0:c,backgroundSize:"cover",backgroundPosition:"center",transition:"opacity ".concat(d,"ms ease-in-out"),zIndex:-1}};return n(t,{children:[e("div",{className:D,style:u(u({},y(m)),{opacity:1})}),_&&v&&e("div",{className:D,style:u(u({},y(v)),{opacity:0})})]})},I=function(){var t=i(s),r=t.getCurrentItem,o=t.getCurrentScene,a=t.handleNext,c=t.handleChoiceSelect,l=t.characters,d=t.handleTypingComplete,g=o(),m=r();if(!g||!m)return null;var h=m.background||g.background,f=m.speaker?l[m.speaker]:null;return n("div",u({className:"".concat(M," flex flex-col"),style:{userSelect:"none"}},{children:[e(E,{src:h}),e("div",u({className:k},{children:f&&e(v,{config:f,emotion:m.emotion,position:m.position||f.defaultPosition,active:!0})})),e("div",u({className:T},{children:n("div",u({className:"w-full max-w-5xl mx-auto px-4 mb-4"},{children:[e(y,{name:null==f?void 0:f.name,text:m.text,textColor:null==f?void 0:f.textColor,onNext:function(){a()},onTypingComplete:d}),m.choices&&e(b,{choices:m.choices,onSelect:c})]}))}))]}))},q=function(n){var t=n.characters,r=n.dialogue,o=n.startScene,a=n.onMessageStart,i=n.onMessage,c=n.onMessageEnd;return e(g,u({characters:t,dialogue:r,startScene:o,onMessageStart:a,onMessage:i,onMessageEnd:c},{children:e(I,{})}))};export{q as ReactDialogic};
//# sourceMappingURL=index.esm.js.map

@@ -1,2 +0,2 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react/jsx-runtime"),n=require("react"),t=function(){return t=Object.assign||function(e){for(var n,t=1,a=arguments.length;t<a;t++)for(var r in n=arguments[t])Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r]);return e},t.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var a=n.createContext({currentScene:null,currentIndex:0,dialogue:{},characters:{},setCurrentScene:function(){},handleNext:function(){},handleChoiceSelect:function(){},getCurrentItem:function(){return null},getCurrentScene:function(){return null},handleTypingComplete:function(){}}),r=function(r){var o=r.children,c=r.dialogue,i=r.characters,s=r.startScene,l=r.onMessageStart,u=r.onMessage,d=r.onMessageEnd,g=n.useState(s||null),f=g[0],m=g[1],h=n.useState(0),x=h[0],v=h[1],p=n.useState(!1),C=p[0],_=p[1],S=n.useCallback((function(){return f&&c[f]?c[f]:null}),[f,c]),y=n.useCallback((function(){var e=S();return!e||!e.sequence||x>=e.sequence.length?null:e.sequence[x]}),[S,x]),b=n.useCallback((function(e,n){m(e),v(n),_(!1);var t=c[e];t&&t.sequence&&t.sequence[n]&&l&&l(t.sequence[n])}),[c,l]),j=n.useCallback((function(){_(!0);var e=y();e&&u&&u(e)}),[y,u]),M=n.useCallback((function(){var e=S();if(e){var n=y();n&&(C?n.choices||(x<e.sequence.length-1?b(f,x+1):(d&&d(n),n.next&&b(n.next,0))):j())}}),[f,x,S,y,C,j,b,d]),k=n.useCallback((function(e){e?b(e,0):b(f,x+1)}),[b]);return e.jsx(a.Provider,t({value:{currentScene:f,currentIndex:x,dialogue:c,characters:i,setCurrentScene:function(e){return b(e,0)},handleNext:M,handleChoiceSelect:k,getCurrentItem:y,getCurrentScene:S,onMessageStart:l,onMessage:u,onMessageEnd:d,handleTypingComplete:j}},{children:o}))},o=function(n){var a=n.children,o=n.characters,c=n.dialogue,i=n.startScene,s=n.onMessageStart,l=n.onMessage,u=n.onMessageEnd;return e.jsx(r,t({characters:o,dialogue:c,startScene:i,onMessageStart:s,onMessage:l,onMessageEnd:u},{children:a}))},c="Character-module_character__WMx3-",i="Character-module_characterEnter__x7Q7O",s="Character-module_characterExit__yGDp3",l=function(a){var r=a.config,o=a.emotion,l=void 0===o?"default":o,u=a.position,d=void 0===u?"center":u,g=a.active,f=void 0===g||g,m=n.useMemo((function(){return r.images[l]||r.images.default}),[r.images,l]),h=n.useMemo((function(){switch(d){case"left":return{left:"var(--dialogic-character-side-margin, 1rem)",right:"auto",transform:"translateX(0)"};case"right":return{left:"auto",right:"var(--dialogic-character-side-margin, 1rem)",transform:"translateX(0)"};default:return{left:"50%",right:"auto",transform:"translateX(-50%)",bottom:"var(--dialogic-character-bottom, 0)"}}}),[d]),x=f?"opacity-100":"opacity-50",v=f?i:s;return e.jsx("div",t({className:"".concat(c," ").concat(x," ").concat(v," pointer-events-auto"),style:t({position:"absolute",height:"center"===d?"auto":"100%",display:"flex",alignItems:"flex-end"},h)},{children:e.jsx("img",{src:m,alt:r.name,className:"center"===d?"w-full h-auto object-contain":void 0})}))};var u="DialogBox-module_dialogBox__--CNd",d="DialogBox-module_characterName__6UKbn",g="DialogBox-module_dialogText__APDO3",f="DialogBox-module_continueIndicator__ZJppK",m=function(a){var r=a.name,o=a.text,c=a.textColor,i=a.isTyping,s=void 0===i||i,l=a.typingSpeed,m=a.onTypingComplete,h=a.onNext,x=function(e,t){void 0===t&&(t={});var a=t.speed,r=void 0===a?30:a,o=t.startTyping,c=void 0===o||o,i=t.onComplete,s=t.punctuationDelay,l=void 0===s?{",":150,".":300,"!":300,"?":300,";":200,":":200}:s,u=n.useState(""),d=u[0],g=u[1],f=n.useState(!1),m=f[0],h=f[1],x=n.useState(0),v=x[0],p=x[1],C=n.useCallback((function(){g(e),p(e.length),h(!0),null==i||i()}),[e,i]);n.useEffect((function(){if(c&&!m){if(0===e.length)return h(!0),void(null==i||i());if(v>=e.length)return console.log("currentIndex >= text.length"),h(!0),void(null==i||i());var n=e[v],t=l[n]||r,a=setTimeout((function(){g(e.substring(0,v+1)),p((function(e){return e+1}))}),t);return function(){return clearTimeout(a)}}}),[e,v,r,c,m,i,l]);var _=n.useCallback((function(){g(""),p(0),h(!1)}),[]);return n.useEffect((function(){_()}),[e,_]),{displayText:d,isComplete:m,complete:C,reset:_}}(o,{speed:l||40,startTyping:s,onComplete:m}),v=x.displayText,p=x.isComplete,C=x.complete;n.useEffect((function(){s||C()}),[s,C]);var _=function(){p?null==h||h():C()};return e.jsxs("div",t({className:u,onClick:_,onKeyDown:function(e){"Enter"!==e.key&&" "!==e.key||_()},tabIndex:0,role:"button","aria-label":"繼續對話"},{children:[r&&e.jsx("div",t({className:d,style:c?{color:c}:void 0},{children:r})),e.jsx("div",t({className:g},{children:v})),p&&e.jsx("div",t({className:f},{children:"▼"}))]}))},h="ChoiceMenu-module_choices__uaEql",x="ChoiceMenu-module_choiceItem__4I3Fk",v=function(n){var a=n.choices,r=n.onSelect;return e.jsx("div",t({className:h},{children:a.map((function(n,a){return e.jsxs("button",t({className:x,onClick:function(){return r(n.next)}},{children:[n.icon&&e.jsx("span",t({className:"mr-2"},{children:n.icon})),e.jsx("span",{children:n.text})]}),"choice-".concat(a))}))}))},p="DialogicContainer-module_container__excEe",C="DialogicContainer-module_charactersContainer__JyNka",_="DialogicContainer-module_dialogArea__0hEYq",S="DialogicContainer-module_background__KDi75",y=function(a){var r=a.src,o=a.color,c=void 0===o?"transparent":o,i=a.transitionDuration,s=void 0===i?500:i,l=n.useState(r),u=l[0],d=l[1],g=n.useState(void 0),f=g[0],m=g[1],h=n.useState(!1),x=h[0],v=h[1];n.useEffect((function(){if(r!==u){m(u),d(r),v(!0);var e=setTimeout((function(){v(!1),m(void 0)}),s);return function(){return clearTimeout(e)}}}),[r,u,s]);var p=function(e){return{position:"absolute",inset:0,backgroundImage:e?"url(".concat(e,")"):"none",backgroundColor:e?void 0:c,backgroundSize:"cover",backgroundPosition:"center",transition:"opacity ".concat(s,"ms ease-in-out"),zIndex:-1}};return e.jsxs(e.Fragment,{children:[e.jsx("div",{className:S,style:t(t({},p(u)),{opacity:1})}),x&&f&&e.jsx("div",{className:S,style:t(t({},p(f)),{opacity:0})})]})},b=function(){var r=n.useContext(a),o=r.getCurrentItem,c=r.getCurrentScene,i=r.handleNext,s=r.handleChoiceSelect,u=r.characters,d=r.handleTypingComplete,g=c(),f=o();if(!g||!f)return null;var h=f.background||g.background,x=f.speaker?u[f.speaker]:null;return e.jsxs("div",t({className:"".concat(p," flex flex-col"),style:{userSelect:"none"}},{children:[e.jsx(y,{src:h}),e.jsx("div",t({className:C},{children:x&&e.jsx(l,{config:x,emotion:f.emotion,position:f.position||x.defaultPosition,active:!0})})),e.jsx("div",t({className:_},{children:e.jsxs("div",t({className:"w-full max-w-5xl mx-auto px-4 mb-4"},{children:[e.jsx(m,{name:null==x?void 0:x.name,text:f.text,textColor:null==x?void 0:x.textColor,onNext:function(){i()},onTypingComplete:d}),f.choices&&e.jsx(v,{choices:f.choices,onSelect:s})]}))}))]}))};exports.ReactDialogic=function(n){var a=n.characters,r=n.dialogue,c=n.startScene,i=n.onMessageStart,s=n.onMessage,l=n.onMessageEnd;return e.jsx(o,t({characters:a,dialogue:r,startScene:c,onMessageStart:i,onMessage:s,onMessageEnd:l},{children:e.jsx(b,{})}))};
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react/jsx-runtime"),n=require("react"),t=function(){return t=Object.assign||function(e){for(var n,t=1,a=arguments.length;t<a;t++)for(var r in n=arguments[t])Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r]);return e},t.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var a=n.createContext({currentScene:null,currentIndex:0,dialogue:{},characters:{},setCurrentScene:function(){},handleNext:function(){},handleChoiceSelect:function(){},getCurrentItem:function(){return null},getCurrentScene:function(){return null},handleTypingComplete:function(){}}),r=function(r){var o=r.children,c=r.dialogue,i=r.characters,s=r.startScene,l=r.onMessageStart,u=r.onMessage,d=r.onMessageEnd,g=n.useState(s||null),f=g[0],m=g[1],h=n.useState(0),x=h[0],v=h[1],p=n.useState(!1),C=p[0],_=p[1],S=n.useCallback((function(){return f&&c[f]?c[f]:null}),[f,c]),y=n.useCallback((function(){var e=S();return!e||!e.sequence||x>=e.sequence.length?null:e.sequence[x]}),[S,x]),b=n.useCallback((function(e,n){m(e),v(n),_(!1);var t=c[e];t&&t.sequence&&t.sequence[n]&&l&&l(t.sequence[n])}),[c,l]),j=n.useCallback((function(){_(!0);var e=y();e&&u&&u(e)}),[y,u]),M=n.useCallback((function(){var e=S();if(e){var n=y();n&&(C?n.choices||(x<e.sequence.length-1?b(f,x+1):(d&&d(n),n.next&&b(n.next,0))):j())}}),[f,x,S,y,C,j,b,d]),k=n.useCallback((function(e){e?b(e,0):b(f,x+1)}),[b,f,x]);return e.jsx(a.Provider,t({value:{currentScene:f,currentIndex:x,dialogue:c,characters:i,setCurrentScene:function(e){return b(e,0)},handleNext:M,handleChoiceSelect:k,getCurrentItem:y,getCurrentScene:S,onMessageStart:l,onMessage:u,onMessageEnd:d,handleTypingComplete:j}},{children:o}))},o=function(n){var a=n.children,o=n.characters,c=n.dialogue,i=n.startScene,s=n.onMessageStart,l=n.onMessage,u=n.onMessageEnd;return e.jsx(r,t({characters:o,dialogue:c,startScene:i,onMessageStart:s,onMessage:l,onMessageEnd:u},{children:a}))},c="Character-module_character__WMx3-",i="Character-module_characterEnter__x7Q7O",s="Character-module_characterExit__yGDp3",l=function(a){var r=a.config,o=a.emotion,l=void 0===o?"default":o,u=a.position,d=void 0===u?"center":u,g=a.active,f=void 0===g||g,m=n.useMemo((function(){return r.images[l]||r.images.default}),[r.images,l]),h=n.useMemo((function(){switch(d){case"left":return{left:"var(--dialogic-character-side-margin, 1rem)",right:"auto",transform:"translateX(0)"};case"right":return{left:"auto",right:"var(--dialogic-character-side-margin, 1rem)",transform:"translateX(0)"};default:return{left:"50%",right:"auto",transform:"translateX(-50%)",bottom:"var(--dialogic-character-bottom, 0)"}}}),[d]),x=f?"opacity-100":"opacity-50",v=f?i:s;return e.jsx("div",t({className:"".concat(c," ").concat(x," ").concat(v," pointer-events-auto"),style:t({position:"absolute",height:"center"===d?"auto":"100%",display:"flex",alignItems:"flex-end"},h)},{children:e.jsx("img",{src:m,alt:r.name,className:"center"===d?"w-full h-auto object-contain":void 0})}))};var u="DialogBox-module_dialogBox__--CNd",d="DialogBox-module_characterName__6UKbn",g="DialogBox-module_dialogText__APDO3",f="DialogBox-module_continueIndicator__ZJppK",m=function(a){var r=a.name,o=a.text,c=a.textColor,i=a.isTyping,s=void 0===i||i,l=a.typingSpeed,m=a.onTypingComplete,h=a.onNext,x=function(e,t){void 0===t&&(t={});var a=t.speed,r=void 0===a?30:a,o=t.startTyping,c=void 0===o||o,i=t.onComplete,s=t.punctuationDelay,l=void 0===s?{",":150,".":300,"!":300,"?":300,";":200,":":200}:s,u=n.useState(""),d=u[0],g=u[1],f=n.useState(!1),m=f[0],h=f[1],x=n.useState(0),v=x[0],p=x[1],C=n.useCallback((function(){g(e),p(e.length),h(!0),null==i||i()}),[e,i]);n.useEffect((function(){if(c&&!m){if(0===e.length)return h(!0),void(null==i||i());if(v>=e.length)return h(!0),void(null==i||i());var n=e[v],t=l[n]||r,a=setTimeout((function(){g(e.substring(0,v+1)),p((function(e){return e+1}))}),t);return function(){return clearTimeout(a)}}}),[e,v,r,c,m,i,l]);var _=n.useCallback((function(){g(""),p(0),h(!1)}),[]);return n.useEffect((function(){_()}),[e,_]),{displayText:d,isComplete:m,complete:C,reset:_}}(o,{speed:l||40,startTyping:s,onComplete:m}),v=x.displayText,p=x.isComplete,C=x.complete;n.useEffect((function(){s||C()}),[s,C]);var _=function(){p?null==h||h():C()};return e.jsxs("div",t({className:u,onClick:_,onKeyDown:function(e){"Enter"!==e.key&&" "!==e.key||_()},tabIndex:0,role:"button","aria-label":"繼續對話"},{children:[r&&e.jsx("div",t({className:d,style:c?{color:c}:void 0},{children:r})),e.jsx("div",t({className:g},{children:v})),p&&e.jsx("div",t({className:f},{children:"▼"}))]}))},h="ChoiceMenu-module_choices__uaEql",x="ChoiceMenu-module_choiceItem__4I3Fk",v=function(n){var a=n.choices,r=n.onSelect;return e.jsx("div",t({className:h},{children:a.map((function(n,a){return e.jsxs("button",t({className:x,onClick:function(){return r(n.next)}},{children:[n.icon&&e.jsx("span",t({className:"mr-2"},{children:n.icon})),e.jsx("span",{children:n.text})]}),"choice-".concat(a))}))}))},p="DialogicContainer-module_container__excEe",C="DialogicContainer-module_charactersContainer__JyNka",_="DialogicContainer-module_dialogArea__0hEYq",S="DialogicContainer-module_background__KDi75",y=function(a){var r=a.src,o=a.color,c=void 0===o?"transparent":o,i=a.transitionDuration,s=void 0===i?500:i,l=n.useState(r),u=l[0],d=l[1],g=n.useState(void 0),f=g[0],m=g[1],h=n.useState(!1),x=h[0],v=h[1];n.useEffect((function(){if(r!==u){m(u),d(r),v(!0);var e=setTimeout((function(){v(!1),m(void 0)}),s);return function(){return clearTimeout(e)}}}),[r,u,s]);var p=function(e){return{position:"absolute",inset:0,backgroundImage:e?"url(".concat(e,")"):"none",backgroundColor:e?void 0:c,backgroundSize:"cover",backgroundPosition:"center",transition:"opacity ".concat(s,"ms ease-in-out"),zIndex:-1}};return e.jsxs(e.Fragment,{children:[e.jsx("div",{className:S,style:t(t({},p(u)),{opacity:1})}),x&&f&&e.jsx("div",{className:S,style:t(t({},p(f)),{opacity:0})})]})},b=function(){var r=n.useContext(a),o=r.getCurrentItem,c=r.getCurrentScene,i=r.handleNext,s=r.handleChoiceSelect,u=r.characters,d=r.handleTypingComplete,g=c(),f=o();if(!g||!f)return null;var h=f.background||g.background,x=f.speaker?u[f.speaker]:null;return e.jsxs("div",t({className:"".concat(p," flex flex-col"),style:{userSelect:"none"}},{children:[e.jsx(y,{src:h}),e.jsx("div",t({className:C},{children:x&&e.jsx(l,{config:x,emotion:f.emotion,position:f.position||x.defaultPosition,active:!0})})),e.jsx("div",t({className:_},{children:e.jsxs("div",t({className:"w-full max-w-5xl mx-auto px-4 mb-4"},{children:[e.jsx(m,{name:null==x?void 0:x.name,text:f.text,textColor:null==x?void 0:x.textColor,onNext:function(){i()},onTypingComplete:d}),f.choices&&e.jsx(v,{choices:f.choices,onSelect:s})]}))}))]}))};exports.ReactDialogic=function(n){var a=n.characters,r=n.dialogue,c=n.startScene,i=n.onMessageStart,s=n.onMessage,l=n.onMessageEnd;return e.jsx(o,t({characters:a,dialogue:r,startScene:c,onMessageStart:i,onMessage:s,onMessageEnd:l},{children:e.jsx(b,{})}))};
//# sourceMappingURL=index.js.map

@@ -16,3 +16,3 @@ /**

*/
next: string;
next?: string;
}

@@ -19,0 +19,0 @@ /**

{
"name": "react-dialogic",
"version": "1.4.2",
"version": "1.4.3",
"description": "一個專為 React 應用開發的對話系統 UI 套件,提供類遊戲風格的對話介面",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet