@@ -15,13 +15,16 @@ import {
1515 registerClass ,
1616 PropertyType ,
1717 getParent ,
18- IMessage
18+ IMessage ,
19+ setParent ,
20+ SerializedData
1921} from "project-editor/core/object" ;
2022import {
2123 ProjectStore ,
2224 IContextMenuContext ,
2325 getProjectStore ,
24- createObject
26+ createObject ,
27+ objectToClipboardData
2528} from "project-editor/store" ;
2629import { replaceObjectReference } from "project-editor/core/search" ;
2730
@@ -88,7 +91,7 @@ const ColorItem = observer(
8891 }
8992
9093 get themeColor ( ) {
91- return this . selectedTheme . colors [ this . colorIndex ] ;
94+ return this . selectedTheme ? .colors [ this . colorIndex ] ?? "#000000" ;
9295 }
9396
9497 changedThemeColor : string | undefined ;
@@ -463,20 +466,12 @@ export class Color extends EezObject {
463466 return color ;
464467 } ,
465468
466- onAfterPaste : ( newColor : Color , fromColor : Color ) => {
467- const project = ProjectEditor . getProject ( newColor ) ;
468-
469- const fromTheme =
470- project . _store . navigationStore . selectedThemeObject . get ( ) as Theme ;
471- if ( fromTheme ) {
472- for ( const theme of project . themes ) {
473- project . setThemeColor (
474- theme . objID ,
475- newColor . objID ,
476- project . getThemeColor ( fromTheme . objID , fromColor . objID )
477- ) ;
478- }
479- }
469+ objectsToClipboardData : ( colors : Color [ ] ) => {
470+ const projectStore = ProjectEditor . getProjectStore ( colors [ 0 ] ) ;
471+ const themeColorsClipboardData = new ThemeColorsClipboardData ( ) ;
472+ themeColorsClipboardData . addColors ( projectStore , colors ) ;
473+ setParent ( themeColorsClipboardData , projectStore . project ) ;
474+ return objectToClipboardData ( projectStore , themeColorsClipboardData ) ;
480475 } ,
481476
482477 extendContextMenu : (
@@ -613,16 +608,12 @@ export class Theme extends EezObject implements ITheme {
613608 return theme ;
614609 } ,
615610
616- onAfterPaste : ( newTheme : Theme , fromTheme : Theme ) => {
617- const project = ProjectEditor . getProject ( newTheme ) ;
618-
619- for ( const color of project . colors ) {
620- project . setThemeColor (
621- newTheme . objID ,
622- color . objID ,
623- project . getThemeColor ( fromTheme . objID , color . objID )
624- ) ;
625- }
611+ objectsToClipboardData : ( themes : Theme [ ] ) => {
612+ const projectStore = ProjectEditor . getProjectStore ( themes [ 0 ] ) ;
613+ const themeColorsClipboardData = new ThemeColorsClipboardData ( ) ;
614+ themeColorsClipboardData . addThemes ( projectStore , themes ) ;
615+ setParent ( themeColorsClipboardData , projectStore . project ) ;
616+ return objectToClipboardData ( projectStore , themeColorsClipboardData ) ;
626617 }
627618 } ;
628619
@@ -667,6 +658,180 @@ registerClass("Theme", Theme);
667658
668659////////////////////////////////////////////////////////////////////////////////
669660
661+ export class ThemeColorsClipboardData extends EezObject {
662+ data : {
663+ kind : "themes" | "colors" ;
664+ themes : {
665+ name : string ;
666+ colors : string [ ] ;
667+ } [ ] ;
668+ colorNames : string [ ] ;
669+ } ;
670+
671+ static classInfo : ClassInfo = {
672+ properties : [
673+ {
674+ name : "data" ,
675+ type : PropertyType . Any
676+ }
677+ ] ,
678+
679+ pasteItemHook : (
680+ object : IEezObject ,
681+ clipboardData : {
682+ serializedData : SerializedData ;
683+ pastePlace : EezObject ;
684+ }
685+ ) => {
686+ const projectStore = ProjectEditor . getProjectStore (
687+ clipboardData . pastePlace
688+ ) ;
689+ const themeColorsClipboardData = clipboardData . serializedData
690+ . object as ThemeColorsClipboardData ;
691+
692+ let closeCombineCommands = false ;
693+ if ( ! projectStore . undoManager . combineCommands ) {
694+ projectStore . undoManager . setCombineCommands ( true ) ;
695+ closeCombineCommands = true ;
696+ }
697+
698+ const newObjects : EezObject [ ] = [ ] ;
699+
700+ if ( themeColorsClipboardData . data . kind == "themes" ) {
701+ for ( const colorName of themeColorsClipboardData . data . colorNames ) {
702+ const color = projectStore . project . colors . find (
703+ color => color . name == colorName
704+ ) ;
705+ if ( ! color ) {
706+ projectStore . addObject (
707+ projectStore . project . colors ,
708+ createObject < Color > (
709+ projectStore ,
710+ {
711+ name : colorName
712+ } ,
713+ Color
714+ )
715+ ) ;
716+ }
717+ }
718+
719+ for ( const theme of themeColorsClipboardData . data . themes ) {
720+ const newTheme = projectStore . addObject (
721+ projectStore . project . themes ,
722+ createObject < Theme > (
723+ projectStore ,
724+ {
725+ name : theme . name
726+ } ,
727+ Theme
728+ )
729+ ) ;
730+
731+ newObjects . push ( newTheme ) ;
732+
733+ for ( const color of projectStore . project . colors ) {
734+ const index =
735+ themeColorsClipboardData . data . colorNames . findIndex (
736+ colorName => colorName == color . name
737+ ) ;
738+ projectStore . project . setThemeColor (
739+ newTheme . objID ,
740+ color . objID ,
741+ index != - 1 ? theme . colors [ index ] : "#000000"
742+ ) ;
743+ }
744+ }
745+ } else {
746+ for ( const theme of themeColorsClipboardData . data . themes ) {
747+ const theme2 = projectStore . project . themes . find (
748+ theme2 => theme2 . name == theme . name
749+ ) ;
750+ if ( ! theme2 ) {
751+ projectStore . addObject (
752+ projectStore . project . themes ,
753+ createObject < Theme > (
754+ projectStore ,
755+ {
756+ name : theme . name
757+ } ,
758+ Theme
759+ )
760+ ) ;
761+ }
762+ }
763+
764+ for (
765+ let colorIndex = 0 ;
766+ colorIndex < themeColorsClipboardData . data . colorNames . length ;
767+ colorIndex ++
768+ ) {
769+ const colorName =
770+ themeColorsClipboardData . data . colorNames [ colorIndex ] ;
771+
772+ const newColor = projectStore . addObject (
773+ projectStore . project . colors ,
774+ createObject < Color > (
775+ projectStore ,
776+ {
777+ name : colorName
778+ } ,
779+ Color
780+ )
781+ ) ;
782+
783+ newObjects . push ( newColor ) ;
784+
785+ for ( const theme of themeColorsClipboardData . data . themes ) {
786+ const theme2 = projectStore . project . themes . find (
787+ theme2 => theme2 . name == theme . name
788+ ) ! ;
789+ projectStore . project . setThemeColor (
790+ theme2 . objID ,
791+ newColor . objID ,
792+ theme . colors [ colorIndex ]
793+ ) ;
794+ }
795+ }
796+ }
797+
798+ if ( closeCombineCommands ) {
799+ projectStore . undoManager . setCombineCommands ( false ) ;
800+ }
801+
802+ return newObjects ;
803+ }
804+ } ;
805+
806+ addThemes ( projectStore : ProjectStore , themes : Theme [ ] ) {
807+ this . data = {
808+ kind : "themes" ,
809+ themes : themes . map ( theme => ( {
810+ name : theme . name ,
811+ colors : theme . colors
812+ } ) ) ,
813+ colorNames : projectStore . project . colors . map ( color => color . name )
814+ } ;
815+ }
816+
817+ addColors ( projectStore : ProjectStore , colors : Color [ ] ) {
818+ this . data = {
819+ kind : "colors" ,
820+ themes : projectStore . project . themes . map ( theme => ( {
821+ name : theme . name ,
822+ colors : colors . map ( color =>
823+ projectStore . project . getThemeColor ( theme . objID , color . objID )
824+ )
825+ } ) ) ,
826+ colorNames : colors . map ( color => color . name )
827+ } ;
828+ }
829+ }
830+
831+ registerClass ( "ThemeColorsClipboardData" , ThemeColorsClipboardData ) ;
832+
833+ ////////////////////////////////////////////////////////////////////////////////
834+
670835function getThemedColorInProject (
671836 project : Project ,
672837 colorValue : string
0 commit comments