🚀 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.3.1

2

dist/index.esm.js

@@ -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}}),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),f=m[0],h=m[1],v=o(0),p=v[0],x=v[1],_=o(!1),C=_[0],y=_[1],S=a((function(){return f&&r[f]?r[f]:null}),[f,r]),M=a((function(){var e=S();return!e||!e.sequence||p>=e.sequence.length?null:e.sequence[p]}),[S,p]),N=a((function(e,n){h(e),x(n),y(!1);var t=r[e];t&&t.sequence&&t.sequence[n]&&l&&l(t.sequence[n])}),[r,l]),b=a((function(){y(!0);var e=M();e&&d&&d(e)}),[M,d]),D=a((function(){var e=S();if(e){var n=M();n&&(C?n.choices||(p<e.sequence.length-1?N(f,p+1):g&&g(n)):b())}}),[f,p,S,M,C,b,N,g]),k=a((function(e){N(e,0)}),[N]);return e(s.Provider,u({value:{currentScene:f,currentIndex:p,dialogue:r,characters:c,setCurrentScene:function(e){return N(e,0)},handleNext:D,handleChoiceSelect:k,getCurrentItem:M,getCurrentScene:S,onMessageStart:l,onMessage:d,onMessageEnd:g}},{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-",f="Character-module_characterEnter__x7Q7O",h="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?f:h;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,f=t.onNext,h=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],f=g[1],h=o(!1),v=h[0],p=h[1],x=o(0),_=x[0],C=x[1],y=a((function(){f(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 p(!0),void(null==u||u());var n=e[_],t=d[n]||r,o=setTimeout((function(){f(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(){f(""),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=h.displayText,y=h.isComplete,S=h.complete;l((function(){d||S()}),[d,S]);var M=function(){y?null==f||f():S()};return n("div",u({className:p,onClick:function(e){e.preventDefault(),M()},onKeyDown:function(e){e.preventDefault(),"Enter"!==e.key&&" "!==e.key||M()},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",M="ChoiceMenu-module_choiceItem__4I3Fk",N=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:M,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))}))}))},b="DialogicContainer-module_container__excEe",D="DialogicContainer-module_charactersContainer__JyNka",k="DialogicContainer-module_dialogArea__0hEYq",E="DialogicContainer-module_background__KDi75",I=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],f=g[1],h=o(void 0),v=h[0],p=h[1],x=o(!1),_=x[0],C=x[1];l((function(){if(a!==m){p(m),f(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:E,style:u(u({},y(m)),{opacity:1})}),_&&v&&e("div",{className:E,style:u(u({},y(v)),{opacity:0})})]})},T=function(){var t=c(s),r=t.getCurrentItem,o=t.getCurrentScene,a=t.handleNext,i=t.handleChoiceSelect,l=t.characters;t.onMessageStart;var d=t.onMessage,g=t.onMessageEnd,m=o(),f=r();if(!m||!f)return null;var h=f.background||m.background,p=f.speaker?l[f.speaker]:null;return n("div",u({className:"".concat(b," flex flex-col"),style:{userSelect:"none"}},{children:[e(I,{src:h}),e("div",u({className:D},{children:p&&e(v,{config:p,emotion:f.emotion,position:f.position||p.defaultPosition,active:!0})})),e("div",u({className:k},{children:n("div",u({className:"w-full max-w-5xl mx-auto px-4 mb-4"},{children:[e(y,{name:null==p?void 0:p.name,text:f.text,textColor:null==p?void 0:p.textColor,onNext:function(){a(),g&&g(f)},onTypingComplete:function(){d&&d(f)}}),f.choices&&e(N,{choices:f.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(T,{})}))};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 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}}),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]),M=a((function(){var e=S();return!e||!e.sequence||p>=e.sequence.length?null:e.sequence[p]}),[S,p]),N=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]),b=a((function(){y(!0);var e=M();e&&d&&d(e)}),[M,d]),k=a((function(){var e=S();if(e){var n=M();n&&(C?n.choices||(p<e.sequence.length-1?N(h,p+1):g&&g(n)):b())}}),[h,p,S,M,C,b,N,g]),E=a((function(e){N(e,0)}),[N]);return e(s.Provider,u({value:{currentScene:h,currentIndex:p,dialogue:r,characters:c,setCurrentScene:function(e){return N(e,0)},handleNext:k,handleChoiceSelect:E,getCurrentItem:M,getCurrentScene:S,onMessageStart:l,onMessage:d,onMessageEnd:g}},{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 M=function(){y?null==h||h():S()};return n("div",u({className:p,onClick:M,onKeyDown:function(e){"Enter"!==e.key&&" "!==e.key||M()},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",M="ChoiceMenu-module_choiceItem__4I3Fk",N=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:M,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))}))}))},b="DialogicContainer-module_container__excEe",k="DialogicContainer-module_charactersContainer__JyNka",E="DialogicContainer-module_dialogArea__0hEYq",D="DialogicContainer-module_background__KDi75",I=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})})]})},T=function(){var t=c(s),r=t.getCurrentItem,o=t.getCurrentScene,a=t.handleNext,i=t.handleChoiceSelect,l=t.characters;t.onMessageStart;var d=t.onMessage,g=t.onMessageEnd,m=o(),h=r();if(!m||!h)return null;var f=h.background||m.background,p=h.speaker?l[h.speaker]:null;return n("div",u({className:"".concat(b," flex flex-col"),style:{userSelect:"none"}},{children:[e(I,{src:f}),e("div",u({className:k},{children:p&&e(v,{config:p,emotion:h.emotion,position:h.position||p.defaultPosition,active:!0})})),e("div",u({className:E},{children:n("div",u({className:"w-full max-w-5xl mx-auto px-4 mb-4"},{children:[e(y,{name:null==p?void 0:p.name,text:h.text,textColor:null==p?void 0:p.textColor,onNext:function(){a(),g&&g(h)},onTypingComplete:function(){d&&d(h)}}),h.choices&&e(N,{choices:h.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(T,{})}))};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"),t=require("react"),n=function(){return n=Object.assign||function(e){for(var t,n=1,a=arguments.length;n<a;n++)for(var r in t=arguments[n])Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e},n.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var a=t.createContext({currentScene:null,currentIndex:0,dialogue:{},characters:{},setCurrentScene:function(){},handleNext:function(){},handleChoiceSelect:function(){},getCurrentItem:function(){return null},getCurrentScene:function(){return null}}),r=function(r){var o=r.children,c=r.dialogue,i=r.characters,s=r.startScene,u=r.onMessageStart,l=r.onMessage,d=r.onMessageEnd,g=t.useState(s||null),f=g[0],m=g[1],h=t.useState(0),v=h[0],x=h[1],p=t.useState(!1),C=p[0],_=p[1],S=t.useCallback((function(){return f&&c[f]?c[f]:null}),[f,c]),b=t.useCallback((function(){var e=S();return!e||!e.sequence||v>=e.sequence.length?null:e.sequence[v]}),[S,v]),j=t.useCallback((function(e,t){m(e),x(t),_(!1);var n=c[e];n&&n.sequence&&n.sequence[t]&&u&&u(n.sequence[t])}),[c,u]),y=t.useCallback((function(){_(!0);var e=b();e&&l&&l(e)}),[b,l]),M=t.useCallback((function(){var e=S();if(e){var t=b();t&&(C?t.choices||(v<e.sequence.length-1?j(f,v+1):d&&d(t)):y())}}),[f,v,S,b,C,y,j,d]),k=t.useCallback((function(e){j(e,0)}),[j]);return e.jsx(a.Provider,n({value:{currentScene:f,currentIndex:v,dialogue:c,characters:i,setCurrentScene:function(e){return j(e,0)},handleNext:M,handleChoiceSelect:k,getCurrentItem:b,getCurrentScene:S,onMessageStart:u,onMessage:l,onMessageEnd:d}},{children:o}))},o=function(t){var a=t.children,o=t.characters,c=t.dialogue,i=t.startScene,s=t.onMessageStart,u=t.onMessage,l=t.onMessageEnd;return e.jsx(r,n({characters:o,dialogue:c,startScene:i,onMessageStart:s,onMessage:u,onMessageEnd:l},{children:a}))},c="Character-module_character__WMx3-",i="Character-module_characterEnter__x7Q7O",s="Character-module_characterExit__yGDp3",u=function(a){var r=a.config,o=a.emotion,u=void 0===o?"default":o,l=a.position,d=void 0===l?"center":l,g=a.active,f=void 0===g||g,m=t.useMemo((function(){return r.images[u]||r.images.default}),[r.images,u]),h=t.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]),v=f?"opacity-100":"opacity-50",x=f?i:s;return e.jsx("div",n({className:"".concat(c," ").concat(v," ").concat(x," pointer-events-auto"),style:n({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 l="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,u=a.typingSpeed,m=a.onTypingComplete,h=a.onNext,v=function(e,n){void 0===n&&(n={});var a=n.speed,r=void 0===a?30:a,o=n.startTyping,c=void 0===o||o,i=n.onComplete,s=n.punctuationDelay,u=void 0===s?{",":150,".":300,"!":300,"?":300,";":200,":":200}:s,l=t.useState(""),d=l[0],g=l[1],f=t.useState(!1),m=f[0],h=f[1],v=t.useState(0),x=v[0],p=v[1],C=t.useCallback((function(){g(e),p(e.length),h(!0),null==i||i()}),[e,i]);t.useEffect((function(){if(c&&!m){if(0===e.length)return h(!0),void(null==i||i());if(x>=e.length)return h(!0),void(null==i||i());var t=e[x],n=u[t]||r,a=setTimeout((function(){g(e.substring(0,x+1)),p((function(e){return e+1}))}),n);return function(){return clearTimeout(a)}}}),[e,x,r,c,m,i,u]);var _=t.useCallback((function(){g(""),p(0),h(!1)}),[]);return t.useEffect((function(){_()}),[e,_]),{displayText:d,isComplete:m,complete:C,reset:_}}(o,{speed:u||40,startTyping:s,onComplete:m}),x=v.displayText,p=v.isComplete,C=v.complete;t.useEffect((function(){s||C()}),[s,C]);var _=function(){p?null==h||h():C()};return e.jsxs("div",n({className:l,onClick:function(e){e.preventDefault(),_()},onKeyDown:function(e){e.preventDefault(),"Enter"!==e.key&&" "!==e.key||_()},tabIndex:0,role:"button","aria-label":"繼續對話"},{children:[r&&e.jsx("div",n({className:d,style:c?{color:c}:void 0},{children:r})),e.jsx("div",n({className:g},{children:x})),p&&e.jsx("div",n({className:f},{children:"▼"}))]}))},h="ChoiceMenu-module_choices__uaEql",v="ChoiceMenu-module_choiceItem__4I3Fk",x=function(t){var a=t.choices,r=t.onSelect;return e.jsx("div",n({className:h},{children:a.map((function(t,a){return e.jsxs("button",n({className:v,onClick:function(){return r(t.next)}},{children:[t.icon&&e.jsx("span",n({className:"mr-2"},{children:t.icon})),e.jsx("span",{children:t.text})]}),"choice-".concat(a))}))}))},p="DialogicContainer-module_container__excEe",C="DialogicContainer-module_charactersContainer__JyNka",_="DialogicContainer-module_dialogArea__0hEYq",S="DialogicContainer-module_background__KDi75",b=function(a){var r=a.src,o=a.color,c=void 0===o?"transparent":o,i=a.transitionDuration,s=void 0===i?500:i,u=t.useState(r),l=u[0],d=u[1],g=t.useState(void 0),f=g[0],m=g[1],h=t.useState(!1),v=h[0],x=h[1];t.useEffect((function(){if(r!==l){m(l),d(r),x(!0);var e=setTimeout((function(){x(!1),m(void 0)}),s);return function(){return clearTimeout(e)}}}),[r,l,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:n(n({},p(l)),{opacity:1})}),v&&f&&e.jsx("div",{className:S,style:n(n({},p(f)),{opacity:0})})]})},j=function(){var r=t.useContext(a),o=r.getCurrentItem,c=r.getCurrentScene,i=r.handleNext,s=r.handleChoiceSelect,l=r.characters;r.onMessageStart;var d=r.onMessage,g=r.onMessageEnd,f=c(),h=o();if(!f||!h)return null;var v=h.background||f.background,S=h.speaker?l[h.speaker]:null;return e.jsxs("div",n({className:"".concat(p," flex flex-col"),style:{userSelect:"none"}},{children:[e.jsx(b,{src:v}),e.jsx("div",n({className:C},{children:S&&e.jsx(u,{config:S,emotion:h.emotion,position:h.position||S.defaultPosition,active:!0})})),e.jsx("div",n({className:_},{children:e.jsxs("div",n({className:"w-full max-w-5xl mx-auto px-4 mb-4"},{children:[e.jsx(m,{name:null==S?void 0:S.name,text:h.text,textColor:null==S?void 0:S.textColor,onNext:function(){i(),g&&g(h)},onTypingComplete:function(){d&&d(h)}}),h.choices&&e.jsx(x,{choices:h.choices,onSelect:s})]}))}))]}))};exports.ReactDialogic=function(t){var a=t.characters,r=t.dialogue,c=t.startScene,i=t.onMessageStart,s=t.onMessage,u=t.onMessageEnd;return e.jsx(o,n({characters:a,dialogue:r,startScene:c,onMessageStart:i,onMessage:s,onMessageEnd:u},{children:e.jsx(j,{})}))};
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react/jsx-runtime"),t=require("react"),n=function(){return n=Object.assign||function(e){for(var t,n=1,a=arguments.length;n<a;n++)for(var r in t=arguments[n])Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e},n.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var a=t.createContext({currentScene:null,currentIndex:0,dialogue:{},characters:{},setCurrentScene:function(){},handleNext:function(){},handleChoiceSelect:function(){},getCurrentItem:function(){return null},getCurrentScene:function(){return null}}),r=function(r){var o=r.children,c=r.dialogue,i=r.characters,s=r.startScene,u=r.onMessageStart,l=r.onMessage,d=r.onMessageEnd,g=t.useState(s||null),f=g[0],m=g[1],h=t.useState(0),x=h[0],v=h[1],p=t.useState(!1),C=p[0],_=p[1],S=t.useCallback((function(){return f&&c[f]?c[f]:null}),[f,c]),b=t.useCallback((function(){var e=S();return!e||!e.sequence||x>=e.sequence.length?null:e.sequence[x]}),[S,x]),j=t.useCallback((function(e,t){m(e),v(t),_(!1);var n=c[e];n&&n.sequence&&n.sequence[t]&&u&&u(n.sequence[t])}),[c,u]),y=t.useCallback((function(){_(!0);var e=b();e&&l&&l(e)}),[b,l]),M=t.useCallback((function(){var e=S();if(e){var t=b();t&&(C?t.choices||(x<e.sequence.length-1?j(f,x+1):d&&d(t)):y())}}),[f,x,S,b,C,y,j,d]),k=t.useCallback((function(e){j(e,0)}),[j]);return e.jsx(a.Provider,n({value:{currentScene:f,currentIndex:x,dialogue:c,characters:i,setCurrentScene:function(e){return j(e,0)},handleNext:M,handleChoiceSelect:k,getCurrentItem:b,getCurrentScene:S,onMessageStart:u,onMessage:l,onMessageEnd:d}},{children:o}))},o=function(t){var a=t.children,o=t.characters,c=t.dialogue,i=t.startScene,s=t.onMessageStart,u=t.onMessage,l=t.onMessageEnd;return e.jsx(r,n({characters:o,dialogue:c,startScene:i,onMessageStart:s,onMessage:u,onMessageEnd:l},{children:a}))},c="Character-module_character__WMx3-",i="Character-module_characterEnter__x7Q7O",s="Character-module_characterExit__yGDp3",u=function(a){var r=a.config,o=a.emotion,u=void 0===o?"default":o,l=a.position,d=void 0===l?"center":l,g=a.active,f=void 0===g||g,m=t.useMemo((function(){return r.images[u]||r.images.default}),[r.images,u]),h=t.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",n({className:"".concat(c," ").concat(x," ").concat(v," pointer-events-auto"),style:n({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 l="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,u=a.typingSpeed,m=a.onTypingComplete,h=a.onNext,x=function(e,n){void 0===n&&(n={});var a=n.speed,r=void 0===a?30:a,o=n.startTyping,c=void 0===o||o,i=n.onComplete,s=n.punctuationDelay,u=void 0===s?{",":150,".":300,"!":300,"?":300,";":200,":":200}:s,l=t.useState(""),d=l[0],g=l[1],f=t.useState(!1),m=f[0],h=f[1],x=t.useState(0),v=x[0],p=x[1],C=t.useCallback((function(){g(e),p(e.length),h(!0),null==i||i()}),[e,i]);t.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 t=e[v],n=u[t]||r,a=setTimeout((function(){g(e.substring(0,v+1)),p((function(e){return e+1}))}),n);return function(){return clearTimeout(a)}}}),[e,v,r,c,m,i,u]);var _=t.useCallback((function(){g(""),p(0),h(!1)}),[]);return t.useEffect((function(){_()}),[e,_]),{displayText:d,isComplete:m,complete:C,reset:_}}(o,{speed:u||40,startTyping:s,onComplete:m}),v=x.displayText,p=x.isComplete,C=x.complete;t.useEffect((function(){s||C()}),[s,C]);var _=function(){p?null==h||h():C()};return e.jsxs("div",n({className:l,onClick:_,onKeyDown:function(e){"Enter"!==e.key&&" "!==e.key||_()},tabIndex:0,role:"button","aria-label":"繼續對話"},{children:[r&&e.jsx("div",n({className:d,style:c?{color:c}:void 0},{children:r})),e.jsx("div",n({className:g},{children:v})),p&&e.jsx("div",n({className:f},{children:"▼"}))]}))},h="ChoiceMenu-module_choices__uaEql",x="ChoiceMenu-module_choiceItem__4I3Fk",v=function(t){var a=t.choices,r=t.onSelect;return e.jsx("div",n({className:h},{children:a.map((function(t,a){return e.jsxs("button",n({className:x,onClick:function(){return r(t.next)}},{children:[t.icon&&e.jsx("span",n({className:"mr-2"},{children:t.icon})),e.jsx("span",{children:t.text})]}),"choice-".concat(a))}))}))},p="DialogicContainer-module_container__excEe",C="DialogicContainer-module_charactersContainer__JyNka",_="DialogicContainer-module_dialogArea__0hEYq",S="DialogicContainer-module_background__KDi75",b=function(a){var r=a.src,o=a.color,c=void 0===o?"transparent":o,i=a.transitionDuration,s=void 0===i?500:i,u=t.useState(r),l=u[0],d=u[1],g=t.useState(void 0),f=g[0],m=g[1],h=t.useState(!1),x=h[0],v=h[1];t.useEffect((function(){if(r!==l){m(l),d(r),v(!0);var e=setTimeout((function(){v(!1),m(void 0)}),s);return function(){return clearTimeout(e)}}}),[r,l,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:n(n({},p(l)),{opacity:1})}),x&&f&&e.jsx("div",{className:S,style:n(n({},p(f)),{opacity:0})})]})},j=function(){var r=t.useContext(a),o=r.getCurrentItem,c=r.getCurrentScene,i=r.handleNext,s=r.handleChoiceSelect,l=r.characters;r.onMessageStart;var d=r.onMessage,g=r.onMessageEnd,f=c(),h=o();if(!f||!h)return null;var x=h.background||f.background,S=h.speaker?l[h.speaker]:null;return e.jsxs("div",n({className:"".concat(p," flex flex-col"),style:{userSelect:"none"}},{children:[e.jsx(b,{src:x}),e.jsx("div",n({className:C},{children:S&&e.jsx(u,{config:S,emotion:h.emotion,position:h.position||S.defaultPosition,active:!0})})),e.jsx("div",n({className:_},{children:e.jsxs("div",n({className:"w-full max-w-5xl mx-auto px-4 mb-4"},{children:[e.jsx(m,{name:null==S?void 0:S.name,text:h.text,textColor:null==S?void 0:S.textColor,onNext:function(){i(),g&&g(h)},onTypingComplete:function(){d&&d(h)}}),h.choices&&e.jsx(v,{choices:h.choices,onSelect:s})]}))}))]}))};exports.ReactDialogic=function(t){var a=t.characters,r=t.dialogue,c=t.startScene,i=t.onMessageStart,s=t.onMessage,u=t.onMessageEnd;return e.jsx(o,n({characters:a,dialogue:r,startScene:c,onMessageStart:i,onMessage:s,onMessageEnd:u},{children:e.jsx(j,{})}))};
//# sourceMappingURL=index.js.map
{
"name": "react-dialogic",
"version": "1.3.0",
"version": "1.3.1",
"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